Reorder List(链表重排序)

leetcode 143. Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln,
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-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}.

分析:这道题的思路首先用快慢指针找到链表的中点,然后将此链表分成两部分,再将后面的链表逆序,最后将两部分合并即可。

第一种解法

class Solution
{
public:
    void reorderList(ListNode* head)
    {
        if(head == nullptr || head->next == nullptr) return;
        ListNode *fastp = head, *lowp = head, *tail = nullptr;
        while(fastp != nullptr && fastp->next != nullptr)
        {
            tail = lowp;
            fastp = fastp->next->next;
            lowp = lowp->next;   //实际上找到的中点,当链表长度为奇数时处于中点,偶数时处于L/2+1位置
        }

        tail->next = nullptr; //分成一半 前半部分在奇数时比后半部分小1
        reverseList(lowp);
        fastp = head;
        tail = nullptr;
        while(fastp != nullptr)
        {
            ListNode *tmp = lowp->next;
            lowp->next = fastp->next;
            fastp->next = lowp;
            tail = lowp;     //奇数时特例,奇数时前半段链表比后半边链表少一个元素
            fastp = lowp->next;
            lowp = tmp;
        }
        if(lowp != nullptr)
            tail->next = lowp;  //把最后一个元素,即中间那个元素添加到链表的最后

    }

    void reverseList(ListNode* &head)
    {
        if(head == nullptr || head->next == nullptr) return;
        ListNode *pre = head, *p = pre->next;
        while(p != nullptr)
        {
            ListNode *tmp = p->next;
            p->next = pre;
            pre = p;
            p = tmp;
        }
        head->next = nullptr;
        head = pre;
    }
}; 

第二种解法

class Solution
{
public:
    void reorderList(ListNode* head)
    {
        ListNode *mid = findMid(head);
        ListNOde *right = reverseList(mid);
        ListNode *left = head;

        while(right && right->next)    //最后一个元素没有进行连接,是因为left最后一个元素本来就连着right的最后一个元素
        {
            ListNode *target = right;
            right = right->next;
            target->next = left->next;
            left->next = target;
            left = target->next;
        }
    }

    ListNode* findMid(ListNode* head)
    {
        ListNode *fast = head;
        ListNode *slow = head;
        while(fast && fast->next)
        {
            fast = fast->next->next
            slow = slow->next;
        }
        return slow
    }

    ListNode* reverseList(ListNode *head)
    {
        if(!head || !head->next) return head;
        ListNode *pre = head, *cur = head->next;
        while(cur)
        {
            ListNode *temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        head->next = nullptr;
        return pre;
    }
};  

第三种解法

public class Solution {
    public void reorderList(ListNode head) {
        if(head == null || head.next == null) return;

        ListNode slow = head;
        ListNode fast = head;
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }

        ListNode mid = slow.next;
        ListNode last = mid;
        ListNode pre = null;
        while(last != null){
            ListNode next = last.next;
            last.next = pre;
            pre = last;
            last = next;
        }
        slow.next = null;

        while(head != null && pre != null){
            ListNode next1 = head.next;
            head.next = pre;
            pre = pre.next;
            head.next.next = next1;
            head = next1;
        }
    }
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值