[LeetCode]143.Reorder List

160 篇文章 28 订阅
【题目】

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}.

【题意】

给定一个单链表L:L0→L1→...→LN-1→LN, 
它重新排列到:L0→LN→L1→LN-1→L2→LN-2→...

【分析】

题目规定要 in-place,也就是说只能使用 O(1) 的空间。
可以找到中间节点,断开,把后半截单链表 reverse 一下,再合并两个单链表。

【代码】

/*********************************
*   日期:2014-01-31
*   作者:SJF0115
*   题目: 143.Reorder List 
*   网址:http://oj.leetcode.com/problems/reorder-list/
*   结果:AC
*   来源:LeetCode
*   总结:
**********************************/
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode* reorderList(ListNode *head) {
        if(head == NULL || head->next == NULL){
            return head;
        }
        //找中间节点
        ListNode *slow = head,*fast = head,*pre = NULL,*pre2 = NULL,*temp = NULL;
        while(fast != NULL && fast->next != NULL){
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        //在中间截断成两个单链表
        pre->next = NULL;
        ListNode *head2 = reverse(slow);
        //合并
        pre = head;
        pre2 = head2;
        while(pre->next != NULL){
            temp = pre2->next;
            //合并
            pre2->next = pre->next;
            pre->next = pre2;
            //下一个元素
            pre = pre->next->next;
            pre2 = temp;
        }
        pre->next = pre2;
        return head;
    }
private:
    ListNode* reverse(ListNode *head){
        if(head == NULL || head->next == NULL){
            return head;
        }

        ListNode *dummy = (ListNode*)malloc(sizeof(ListNode));
        dummy->next = head;

        ListNode *tail = head,*cur = head->next;
        while(cur != NULL){
            //删除cur元素
            tail->next = cur->next;
            //插入
            cur->next = dummy->next;
            dummy->next = cur;
            cur = tail->next;
        }
        return dummy->next;
    }
};

int main() {
    Solution solution;
    int A[] = {1,2,3,4,5,6};
    ListNode *head = (ListNode*)malloc(sizeof(ListNode));
    head->next = NULL;
    ListNode *node;
    ListNode *pre = head;
    for(int i = 0;i < 6;i++){
        node = (ListNode*)malloc(sizeof(ListNode));
        node->val = A[i];
        node->next = NULL;
        pre->next = node;
        pre = node;
    }
    head = solution.reorderList(head->next);
    while(head != NULL){
        printf("%d ",head->val);
        head = head->next;
    }
    //printf("Result:%d\n",result);
    return 0;
}


【温故】

/*------------------------------------------------------
*   日期:2014-04-09
*   作者:SJF0115
*   题目: 143.Reorder List
*   网址:http://oj.leetcode.com/problems/reorder-list/
*   结果:AC
*   来源:LeetCode
*   总结:
--------------------------------------------------------*/
#include <iostream>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode* reorderList(ListNode *head) {
        if(head == nullptr || head->next == nullptr){
            return head;
        }//if
        // 寻找中间节点
        ListNode *slow = head,*fast = head;
        while(fast->next && fast->next->next){
            slow = slow->next;
            fast = fast->next->next;
        }//while
        // 从中间节点截断,分成两个子链表
        ListNode *head2 = slow->next;
        slow->next = nullptr;
        head2 = Reverse(head2);
        // 合并
        slow = head;
        fast = head2;
        ListNode *nextNode;
        while(slow->next){
            nextNode = fast->next;
            // 连接到前半链表
            fast->next = slow->next;
            slow->next = fast;
            // 更新链表指针
            slow = slow->next->next;
            fast = nextNode;
        }//while
        slow->next = fast;
        return head;
    }
private:
    ListNode* Reverse(ListNode* head){
        if(head == nullptr){
            return nullptr;
        }//if
        // 头结点
        ListNode* dummy = new ListNode(0);
        ListNode* cur = head,*nextNode = nullptr;
        while(cur){
            nextNode = cur->next;
            // 头插入法
            cur->next = dummy->next;
            dummy->next = cur;
            cur = nextNode;
        }//while
        return dummy->next;
    }
};

int main() {
    Solution solution;
    int A[] = {1,2,3,4,5,6};
    ListNode* head = new ListNode(-1);
    ListNode *pre = head,*node;
    for(int i = 0;i < 6;i++){
        node = new ListNode(A[i]);
        pre->next = node;
        pre = node;
    }//for

    head = solution.reorderList(head->next);

    while(head != NULL){
        cout<<head->val<<" ";
        head = head->next;
    }//while
    cout<<endl;
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@SmartSi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值