forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
111 lines (90 loc) · 2.66 KB
/
main.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
# Reverse Nodes in k-Group
# Given a linked list, reverse the nodes of a linked list k at a time and
# return its modified list.
# If the number of nodes is not a multiple of k then left-out nodes in the
# end should remain as it is.
# You may not alter the values in the nodes, only nodes itself may be changed.
# Only constant memory is allowed.
# For example,
# Given this linked list: 1->2->3->4->5
# For k = 2, you should return: 2->1->4->3->5
# For k = 3, you should return: 3->2->1->4->5
'''
Created on 2013-5-20
@author: Yubin Bai
'''
class ListNode:
def __init__(self, val):
self.next = None
self.val = val
def generateList(data):
if len(data) == 0:
return None
head = ListNode(data[0])
curr = head
for i in range(1, len(data)):
curr.next = ListNode(data[i])
curr = curr.next
return head
def printList(head):
curr = head
result = []
while curr != None:
result.append(curr.val)
curr = curr.next
print(result)
class Solution:
# @param head, a ListNode
# @param k, an integer
# @return a ListNode
def reverseKGroup(self, head, k):
if head == None:
return None
newList = head
newListTail = None
currHead = currTail = head
counter = 1
while currHead != None:
currTemp = currHead
currHead = currHead.next
if counter == k:
nextHead, nextTail = self.reverseList(currTail, currTemp)
if newList == head:
newList = nextHead
newListTail = nextTail
nextTail.next = None
else:
newListTail.next = nextHead
newListTail = nextTail
newListTail.next = None
currTail = currHead
counter = 0
counter += 1
if newListTail != None:
newListTail.next = currTail
return newList
def reverseList(self, head, tail):
if head == None:
return None
if head == tail:
return head
oldList = head.next
newList = head
newList.next = None
while oldList != tail:
curr = oldList
oldList = curr.next
curr.next = newList
newList = curr
tail.next = newList
return tail, head
if __name__ == '__main__':
sol = Solution()
# data = list(range(10))
data = [1]
linked = generateList(data)
printList(linked)
linked2 = sol.reverseKGroup(linked, 5)
printList(linked2)
linked2 = sol.reverseKGroup(linked2, 5)
printList(linked2)