Reorder List

You are given the head of a singly linked-list. Reorder the list so the nodes appear in the order L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ..., where you cannot modify the values in the nodes, only the nodes themselves.

Constraints

  • The number of nodes in the list is in the range [1, 50000].
  • 1 <= Node.val <= 1000
Saving… · 8 lines
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next

class Solution:
def reorderList(self, head: ListNode) -> None:
Console

Click Run to execute sample tests.