Last active
April 11, 2020 05:30
-
-
Save RP-3/53acaa8764978b84855485bd6806d63d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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