Created
October 5, 2019 07:15
-
-
Save sheldonrobinson/05897cfecca0d32936c9711c8a64c4bb to your computer and use it in GitHub Desktop.
CodeSignal Solution rearrangeLastN
This file contains 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
# Definition for singly-linked list: | |
# class ListNode(object): | |
# def __init__(self, x): | |
# self.value = x | |
# self.next = None | |
# | |
def rearrangeLastN(l, n): | |
if n == 0: | |
return l | |
front, back = l, l | |
for _ in range(n): | |
front = front.next | |
if not front: | |
return l | |
while front.next: | |
front = front.next | |
back = back.next | |
out = back.next | |
back.next = None | |
front.next = l | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment