-
Notifications
You must be signed in to change notification settings - Fork 43
/
odd-even-linked-list.py
127 lines (117 loc) · 3.33 KB
/
odd-even-linked-list.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.
You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.
Example:
Given 1->2->3->4->5->NULL,
return 1->3->5->2->4->NULL.
1
2
3
Note:
The relative order inside both the even and odd groups should remain as it was in the input.
The first node is considered odd, the second node even and so on …
"""
# V0
class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
odd = ListNode(0)
even = ListNode(0)
oddHead, evenHead = odd, even
index = 0
while head:
if index & 1 == 0:
odd.next = head
odd = odd.next
else:
even.next = head
even = even.next
head = head.next
index += 1
even.next = None
odd.next = evenHead.next
return oddHead.next
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/79569396
class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
odd = ListNode(0)
even = ListNode(0)
oddHead, evenHead = odd, even
index = 0
while head:
if index & 1 == 0:
odd.next = head
odd = odd.next
else:
even.next = head
even = even.next
head = head.next
index += 1
even.next = None
odd.next = evenHead.next
return oddHead.next
# V2 : DEV
# https://blog.csdn.net/fuxuemingzhu/article/details/79569396
class ListNode:
def __init__(self, data):
"constructor to initiate this object"
# store data
self.data = data
# store reference (next item)
self.next = None
return
def has_value(self, value):
"method to compare the value with the node data"
if self.data == value:
return True
else:
return False
class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
odd = ListNode(0)
even = ListNode(0)
oddHead, evenHead = odd, even
index = 0
while head:
if index & 1 == 0:
odd.next = head
odd = odd.__next__
else:
even.next = head
even = even.__next__
head = head.__next__
index += 1
even.next = None
odd.next = evenHead.__next__
return oddHead.__next__
# V3
# Time: O(n)
# Space: O(1)
class Solution(object):
def oddEvenList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if head:
odd_tail, cur = head, head.__next__
while cur and cur.__next__:
even_head = odd_tail.__next__
odd_tail.next = cur.__next__
odd_tail = odd_tail.__next__
cur.next = odd_tail.__next__
odd_tail.next = even_head
cur = cur.__next__
return head