-
Notifications
You must be signed in to change notification settings - Fork 0
/
61.py
48 lines (41 loc) · 1.37 KB
/
61.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
"""
k會以ListNode的長度為一循環, 因此求餘數即可表k,
如範例head = [1, 2, 3, 4, 5], k = 2 -> [4, 5, 1, 2, 3],
視同把head分成 [1, 2, 3] [4, 5], 接著把[1, 2, 3]接到[4, 5]的尾
1. 先求出總數, 簡化k的數量
2. 緩存目前所需要的前段ListNode
3. 遍歷剩下的ListNode走到最一個元素後, 接上
"""
def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
if not head:
return None
if not k:
return head
walker = ListNode()
walker.next = head
count = 0
while walker:
walker = walker.next
count += 1
count -= 1 # 前面是從0開始, 因此要-1
k %= count # 求餘數
k = count - k # 扣掉後才是前段需要緩存的ListNode
tmp = ListNode(val=0, next=None)
dummy = tmp
for _ in range(k):
tmp.next = ListNode(val=head.val)
head = head.next
tmp = tmp.next
if not head:
return dummy.next
walker = head
while walker.next:
walker = walker.next
walker.next = dummy.next
return head