LeetCode 143. Reorder List

本文介绍了一种将链表L0→L1→…→Ln-1→Ln重新排序为L0→Ln→L1→Ln-1→L2→Ln-2→…的算法。该算法分为三个步骤:首先将链表从中间分割成两部分,接着反转后半部分的链表,最后交替合并两部分链表。提供了两种实现方式,一种使用递归但会遇到栈溢出问题,另一种使用迭代降低了空间复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a singly linked list LL0L1→…→Ln-1Ln,
reorder it to: L0LnL1Ln-1L2Ln-2→…

You must do this in-place without altering the nodes' values.

For example,

Given {1,2,3,4}, reorder it to {1,4,2,3}.

这道题的思路是

1,将链表从中间分成两个。

2,对后面的链表进行Reverse。

3,将两个链表交替合并。

我一开始采用递归实现,结果StackOverFlow了,这是由于递归的空间复杂度为O(N)。代码如下。

public class ReOrderList2 {
    ListNode end = null;
    private ListNode reverseList(ListNode head){
        if(head.next != null){
            ListNode temp = reverseList(head.next);
            temp.next = head;
            head.next = null;
        }else{
            end = head;
        }
        return head;
    }
    private ListNode mergeList(ListNode l1, ListNode l2){

        ListNode head = l1 , prevL1 = null, prevL2 = null;
        while(l1!= null && l2 != null){
            prevL1 = l1;
            prevL2 = l2;
            ListNode temp1 = l1.next;
            l1.next = l2;
            l1 = l2 ;
            l2 = temp1;

        }
      //  if(l2 == null) prevL1.next = l1;
   //     if(l1 == null) prevL2.next = l2;
        return head;
    }
    public void reorderList(ListNode head) {
        if(head == null  || head.next == null) return ;
        ListNode fast = head, slow = head, prev = null;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            prev = slow;
            slow = slow.next;
        }
        if(prev != null)prev.next = null;
        reverseList(slow);
        mergeList(head, end);
    }
}
那就将递归改成递推,使得空间复杂度降低为O(1)。

public class Solution {
    private ListNode reverseList(ListNode head){
        ListNode tail = head;
        if(head.next == null) return head;
        ListNode prev =head, cur =head.next, next = cur.next;
        while(cur != null){
            next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }
        tail.next = null;
        return prev;
    }
    private ListNode mergeList(ListNode l1, ListNode l2){
      
        ListNode head = l1;
        while(l1!= null && l2 != null){
           ListNode temp1 = l1.next;
           l1.next = l2;
           l1 = l2 ;
           l2 = temp1;
          
        }
        
        return head;
    }
    public void reorderList(ListNode head) {
        if(head == null  || head.next == null) return ;
        ListNode fast = head, slow = head, prev = null;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            prev = slow;
            slow = slow.next;
        }
        if(prev != null)prev.next = null;
        ListNode last = reverseList(slow);
        mergeList(head, last);
    }
}


上述两个算法的整体时间复杂度均为O(1).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值