-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0061-rotate-list.py
More file actions
34 lines (27 loc) · 824 Bytes
/
Copy path0061-rotate-list.py
File metadata and controls
34 lines (27 loc) · 824 Bytes
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
# https://leetcode.com/problems/rotate-list/
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if (not k or not head or not head.next):
return head
len = 1
tail = head
newHead = head
temp = head
while (tail.next):
len+=1
tail = tail.next
if (not k%(len)):
return head
newTail = temp
for i in range(len-k%(len)-1):
newHead = newHead.next
temp = temp.next
newHead = newHead.next
temp.next = None
tail.next = newTail
return newHead