forked from Azureki/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lc328.py
47 lines (37 loc) · 929 Bytes
/
lc328.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
# 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
"""
odd = head
if not head.next:
return head
evenhead = even = head.next
while even:
odd.next = even.next
if odd.next:
odd = odd.next
even.next = odd.next
even = even.next
odd.next = evenhead
return head
def test(s):
g = (int(x) for x in s.split('->'))
head = ListNode(0)
tail = head
for i in g:
tail.next = node = ListNode(i)
tail = tail.next
soln=Solution()
soln.oddEvenList(head.next)
while(head):
print(head.val)
head = head.next
s = '1->2->3->4->5'
test(s)