-
Notifications
You must be signed in to change notification settings - Fork 0
/
328.py
41 lines (34 loc) · 912 Bytes
/
328.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = ['"wuyadong" <[email protected]>']
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
root_odd = ListNode(0)
odd_head = root_odd
root_even = ListNode(0)
even_head = root_even
p = head
i = 1
while p:
temp = p.next
if i % 2 == 1:
odd_head.next = p
p.next = None
odd_head = p
else:
even_head.next = p
p.next = None
even_head = p
i += 1
p = temp
odd_head.next = root_even.next
return root_odd.next