-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathRotateList.py
More file actions
32 lines (30 loc) · 784 Bytes
/
RotateList.py
File metadata and controls
32 lines (30 loc) · 784 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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param head, a ListNode
# @param k, an integer
# @return a ListNode
def len(self,head):
if head is None:
return 0
return 1+self.len(head.next)
def advance(self,head,n):
if n==1:
return head
return self.advance(head.next,n-1)
def rotateRight(self, head, k):
if head is None:
return head
n=self.len(head)
k=k%n
if k==0:
return head
mid=self.advance(head,n-k)
last=self.advance(head,n)
last.next=head
head=mid.next
mid.next=None
return head