[NeetCode 150] Reorder Linked List

Reorder Linked List

You are given the head of a singly linked-list.

The positions of a linked list of length = 7 for example, can intially be represented as:

[0, 1, 2, 3, 4, 5, 6]

Reorder the nodes of the linked list to be in the following order:

[0, 6, 1, 5, 2, 4, 3]

Notice that in the general case for a list of length = n the nodes are reordered to be in the following order:

[0, n-1, 1, n-2, 2, n-3, …]

You may not modify the values in the list’s nodes, but instead you must reorder the nodes themselves.

Example 1:

Input: head = [2,4,6,8]

Output: [2,8,4,6]

Example 2:

Input: head = [2,4,6,8,10]

Output: [2,10,4,8,6]

Constraints:

1 <= Length of the list <= 1000.
1 <= Node.val <= 1000

Solution

The process of reorder can be divided into 3 steps:

  1. Split the linked list into 2 parts: [0, L//2-1], [L//2, L-1]
  2. Reverse the latter linked list: [0, L//2-1], [L-1, L//2]
  3. Merge two linked lists in an alternating manner.

An elegant way to split the linked list is to use 2 pointers with different speed. Slow pointer moves 1 node each step and fast one moves 2 node each step. When the fast pointer arrives the end or NULL node, the slow pointer will arrive L//2+1

Code

# 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: Optional[ListNode]) -> None:
        slow, fast = head, head.next  # fast=head also works
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        
        half = slow.next
        slow.next = None  # IMPORTANT! Cut the former half
        last = None
        while half:
            nxt = half.next
            half.next = last
            last = half
            half = nxt
        
        pre = head
        while last:
            pre_nxt = pre.next
            last_nxt = last.next
            pre.next = last
            last.next = pre_nxt
            pre = pre_nxt
            last = last_nxt
                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值