-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRevLinlkList.py
58 lines (50 loc) · 1.33 KB
/
RevLinlkList.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
# class LLNode:
# def __init__(self, val, next=None):
# self.val = val
# self.next = next
class Solution:
def reverse(self, head):
prev=None
curr=head
while(curr):
curr_next=curr.next
curr.next=prev
prev=curr
curr=curr_next
rev_head=prev
rev_tail=head
return rev_head,rev_tail
def solve(self, root, k):
if root==None: return root
if k==1 or k==0: return root
count=1
pre=LLNode(-1)
head=root
node=root
new_root=pre
while(node):
if count==k:
post=node.next
node.next=None
rev_h,rev_t=self.reverse(head)
pre.next=rev_h
rev_t.next=post
pre=rev_t
node=post
head=node
count=1
else:
count+=1
prev=node
node=node.next
if count<=k and count>1:
node=prev
node.next=None
rev_h,rev_t=self.reverse(head)
pre.next=rev_h
rev_t.next=None
if new_root.next:
return new_root.next
else:
rev_h,rv_t= self.reverse(root)
return rev_h