Skip to content

Instantly share code, notes, and snippets.

@RP-3
Last active April 11, 2020 05:30
Show Gist options
  • Save RP-3/53acaa8764978b84855485bd6806d63d to your computer and use it in GitHub Desktop.
Save RP-3/53acaa8764978b84855485bd6806d63d to your computer and use it in GitHub Desktop.
class Solution:
def rotateRight(self, head: ListNode, k: int) -> ListNode:
if not head: return None
# get the length of the list
length, current = 0, head
while current:
length+=1
current = current.next
k%=length
if not k: return head
# mod k so k < length of list
current, front = head, head
while k:
front = front.next
k-=1
# advance front by k
while front.next:
current = current.next
front = front.next
# splice and return the new head
newHead = current.next
current.next = None
front.next = head
return newHead
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment