-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path061. Rotate List
45 lines (29 loc) · 959 Bytes
/
061. Rotate List
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
#!/usr/bin/env python3
__author__ = 'Wei Mu'
# 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 == None or k == 0:
return head
pointer = head
length = 1
while pointer.next != None:
length += 1
pointer = pointer.next
k = k % length
if length == 1 or k == 0:
return head
start = ListNode(0)
end_Node = pointer
pointer2 = head
for i in range(length-k):
pointer = pointer2
pointer2 = pointer2.next
start.next = pointer2
pointer.next = None
end_Node.next = head
return start.next