-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path328.py
57 lines (52 loc) · 1.72 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
__________________________________________________________________________________________________
sample 36 ms submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
cur = head
even = ListNode(-1)
evenhead = even
while cur:
even.next = cur.next
even = even.next
if not cur.next or not cur.next.next:
cur.next = evenhead.next
break
cur.next = cur.next.next
cur = cur.next
return head
__________________________________________________________________________________________________
sample 14920 kb submission
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def oddEvenList(self, head: ListNode) -> ListNode:
if not head:
return head
ans = head
odd, even, d_o, d_e = head, head.next, head, head.next
while d_o or d_e:
if d_o:
if d_o.next:
d_o.next = d_o.next.next
d_o = d_o.next
else:
d_o = None
if d_e:
if d_e.next:
d_e.next = d_e.next.next
d_e = d_e.next
else:
d_e = None
while odd.next:
odd = odd.next
odd.next = even
return ans
__________________________________________________________________________________________________