-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path61.py
59 lines (51 loc) · 1.69 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
49
50
51
52
53
54
55
56
57
58
59
__________________________________________________________________________________________________
sample 28 ms submission
# 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 not head:
return None
last = head
length = 1
while last.next:
last = last.next
length = length + 1
k = k % length
last.next = head
temp = head
for _ in range(length - k - 1):
temp = temp.next
answer = temp.next
temp.next = None
return answer
__________________________________________________________________________________________________
sample 13028 kb submission
# 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):
return None
list = [head]
n = 1
current_node = head
result = head
while (current_node.next != None):
n = n + 1
current_node = current_node.next
list.append(current_node)
current_node.next = head
rotations = n - k % n
if (rotations != 0):
current_node = list[rotations - 1]
result = current_node.next
current_node.next = None
return result
__________________________________________________________________________________________________