Skip to content

Instantly share code, notes, and snippets.

@sheldonrobinson
Created October 5, 2019 07:15
Show Gist options
  • Save sheldonrobinson/05897cfecca0d32936c9711c8a64c4bb to your computer and use it in GitHub Desktop.
Save sheldonrobinson/05897cfecca0d32936c9711c8a64c4bb to your computer and use it in GitHub Desktop.
CodeSignal Solution rearrangeLastN
# 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