LeetCode Reorder List

原题链接在这里:https://leetcode.com/problems/reorder-list/

首先想到Method 1是先设置mark=head, 然后每次翻转mark后面的list, 链接mark与反转后新的head, 之后mark = mark.next.直到mark.next == null, Time O(n^2), Space O(1). 这种方法TLE了。

Method 2 先找到中点,断开后反转后半段,在merge两端即可。Time O(n), Space O(1).

Note: 1. 找到中点后,不要忘记断开.

2. 后半段的长度小于等于前半段的长度,所以merge时终止条件是rightHead!=null.

AC Java:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void reorderList(ListNode head) {
        /*
        //Method 1
        if(head == null || head.next == null || head.next.next == null){
            return;
        }
        ListNode mark = head;
        while(mark.next != null){
            ListNode cur = mark.next;
            ListNode pre;
            ListNode tail = mark.next;
            ListNode temp;
            while(tail.next != null){
                pre = cur;
                cur = tail.next;
                temp = cur.next;
                cur.next = pre;
                tail.next = temp;
            }
            mark.next = cur;
            mark = mark.next;
        }
        */
        
        //Method 2
        if(head == null || head.next == null || head.next.next == null){
            return;
        }
        ListNode walker = head;
        ListNode runner = head;
        while(runner.next != null && runner.next.next != null){
            runner = runner.next.next;
            walker = walker.next;
        }
        
        ListNode rightHead = walker.next;
        walker.next = null;
        rightHead = reverseList(rightHead);
        ListNode mark = head;
        
        while(rightHead != null){
            ListNode temp = rightHead.next;
            rightHead.next = mark.next;
            mark.next = rightHead;
            mark = mark.next.next;
            rightHead = temp;
        }
    }
    private ListNode reverseList(ListNode head){
        ListNode tail = head;
        ListNode cur = tail;
        ListNode temp;
        ListNode pre;
        while(tail.next != null){
            pre = cur;
            cur = tail.next;
            temp = cur.next;
            cur.next = pre;
            tail.next = temp;
        }
        return cur;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值