#143
Reorder List
medium· Linked Listruns: 0You are given the head of a singly linked list L0 -> L1 -> ... -> Ln-1 -> Ln. Reorder the list to be of the form L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 -> .... You may not modify the values in the list's nodes; only the nodes themselves may be changed.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 691
class Solution:
def reorderList(self, head: ListNode | None) -> None:
if not head or not head.next:
return
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
second = slow.next
slow.next = None
prev = None
while second:
nxt = second.next
second.next = prev
prev = second
second = nxt
first, second = head, prev
while second:
tmp1, tmp2 = first.next, second.next
first.next = second
second.next = tmp1
first = tmp1
second = tmp2
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.