-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotate-list.py
44 lines (40 loc) · 1.1 KB
/
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
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
#
# Input: 1->2->3->4->5->NULL, k = 2
# Output: 4->5->1->2->3->NULL
#
# This problem is from LeetCode https://leetcode.com/explore/learn/card/linked-list/213/conclusion/1295/
#
class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
node = head
amount = 0
lastNode = None
while(node):
amount += 1
if not node.next:
lastNode = node
node = node.next
if not head or k % amount is 0:
return head
node = head
newHead = head
iteration = 1
while(node):
if iteration is amount - (k % amount):
newHead = node.next
node.next = None
lastNode.next = head
break
node = node.next
iteration += 1
return newHead