-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy path61_RotateList.py
More file actions
66 lines (48 loc) · 1.44 KB
/
Copy path61_RotateList.py
File metadata and controls
66 lines (48 loc) · 1.44 KB
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
# coding: utf8
"""
题目链接: https://leetcode.com/problems/rotate-list/description.
题目描述:
Given a list, rotate the list to the right by k places, where k is non-negative.
Example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
"""
# 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 not head or not head.next or k == 0:
return head
def reverse_partial_list(node, start, end):
"""反转指定区间的链表"""
fake_head = ListNode(0)
fake_head.next = node
pre = fake_head
for _ in range(1, start):
pre = pre.next
ptr = pre.next
ctr = ptr.next
for _ in range(0, end - start):
ptr.next = ctr.next
ctr.next = pre.next
pre.next = ctr
ctr = ptr.next
return fake_head.next
ll = 0
p = head
while p:
ll += 1
p = p.next
k %= ll
l1 = reverse_partial_list(head, 1, ll)
l2 = reverse_partial_list(l1, 1, k)
l3 = reverse_partial_list(l2, k + 1, ll)
return l3