-
Notifications
You must be signed in to change notification settings - Fork 0
/
061_rotate-list.py
93 lines (73 loc) · 1.88 KB
/
061_rotate-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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@author: rye
#@time: 2019/3/27
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if head == None or k == 0:
return head
dumy = ListNode(-1)
dumy.next = head
p = dumy
q = dumy
length = 0
cnt = 0
# 真的不懂这个while循环怎么就超时了
while q.next:
length += 1
q = q.next
k = k % length
q = dumy
while q.next:
if cnt < k:
cnt += 1
else:
p = p.next
q = q.next
q.next = dumy.next
dumy.next = p.next
return dumy.next
class Solution1(object):
def rotateRight(self, head, k):
if head == None or k == 0:
return head
cur = head
size = 1
while cur.next:
size += 1
cur = cur.next
tail = cur
k = k % size
p = self.findKth(head, k)
tail.next = head
head = p.next
p.next = None
return head
def findKth(self, head, k):
dummy = ListNode(-1)
dummy.next = head
p = dummy
q = dummy
for i in range(k):
q = q.next
while q.next:
p = p.next
q = q.next
return p
if __name__ == '__main__':
l1 = ListNode(1)
l1.next = ListNode(2)
l1.next.next = ListNode(3)
l1.next.next.next = ListNode(4)
l1.next.next.next.next = ListNode(5)
print(Solution().rotateRight(l1, 2))