-
Notifications
You must be signed in to change notification settings - Fork 481
/
0328.py
49 lines (42 loc) · 1.07 KB
/
0328.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
42
43
44
45
46
47
48
49
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head == None or head.next == None:
return head
odd = head
even = head.next
t = even
while even != None and even.next != None:
odd.next = even.next
odd = odd.next
even.next = odd.next
even = even.next
odd.next = t
return head
def createList():
head = ListNode(0)
cur = head
cur.next = ListNode(0)
cur.next.next = ListNode(1)
cur.next.next.next = ListNode(1)
cur.next.next.next.next = ListNode(2)
return head
def printList(head):
cur = head
while cur != None:
print(cur.val, '-->', end='')
cur = cur.next
print('NULL')
if __name__ == "__main__":
head = createList()
printList(head)
res = Solution().oddEvenList(head)
printList(res)