-
Notifications
You must be signed in to change notification settings - Fork 1
/
61.rotate-list.py
65 lines (60 loc) · 1.86 KB
/
61.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
#
# date: 2019-09-10 23:43:43
# 思路:一开始可以相当的办法是,封装一个rotateOnce的方法,k有几次,就调用几次方法。优点: 逻辑简单,缺点: 计算复杂
# 另一种方法是,将链表看作一个环形链表,从尾部开始移动k个指针,然后遍历输出。环形链表可以通过数组求余数模拟, 这种方法还是有点性能浪费
# @lc app=leetcode id=61 lang=python3
#
# [61] Rotate List
#
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if head is None:
return head
if head.next is None:
return head
if k == 0:
return head
if k is None:
return head
nodes = []
while head is not None:
nodes.append(head)
head = head.next
nodeLen = len(nodes)
firstIndex = nodeLen - k % nodeLen
outList = None
preNode = outList
for node in nodes[firstIndex:]:
curNode = ListNode(node.val)
if outList is None:
outList = preNode = curNode
preNode.next = curNode
preNode = curNode
for node in nodes[:firstIndex]:
curNode = ListNode(node.val)
if outList is None:
outList = preNode = curNode
preNode.next = curNode
preNode = curNode
return outList
# s = Solution()
# arr = [1,2]
# k = 2
# head = None
# preNode = None
# for i in arr:
# curNode = ListNode(i)
# if head is None:
# head = preNode = curNode
# continue
# preNode.next = curNode
# preNode = curNode
# out = s.rotateRight(head,k)
# while out is not None:
# print(out.val)
# out = out.next