1、重排链表(力扣143)

作者: Turbo时间限制: 1S章节: DS:数组和链表

晚于: 2020-07-08 12:00:00后提交分数乘系数50%

截止日期: 2020-07-15 12:00:00

问题描述 :

给定一个单链表 L:L0→L1→…→Ln-1→Ln ,

将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…

 

你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。

 

示例 1:

给定链表 1->2->3->4, 重新排列为 1->4->2->3.

示例 2:

给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.

 

可使用以下代码,完成其中的reorderList函数,其中形参head指向无头结点单链表。

#include<iostream>

using namespace std;

 

struct ListNode

{

    int val;

    ListNode *next;

    ListNode() : val(0), next(NULL) {}

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

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

};

 

class Solution

{

public:

    void reorderList(ListNode* head)

    {

             //填充本函数完成功能

    }

};

ListNode *createByTail()

{

    ListNode *head;

    ListNode *p1,*p2;

    int n=0,num;

    int len;

    cin>>len;

    head=NULL;

    while(n<len && cin>>num)

    {

        p1=new ListNode(num);

        n=n+1;

        if(n==1)

            head=p1;

        else

            p2->next=p1;

        p2=p1;

    }

    return head;

}

 

void  displayLink(ListNode *head)

{

    ListNode *p;

    p=head;

    cout<<"head-->";

    while(p!= NULL)

    {

        cout<<p->val<<"-->";

        p=p->next;

    }

    cout<<"tail\n";

}

int main()

{

    ListNode* head = createByTail();

    Solution().reorderList(head);

    displayLink(head);

    return 0;

}

#include<iostream>
using namespace std;

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

class Solution//将链表分两半,将第二个逆序,在依次连接
{
public:
    void reorderList(ListNode* head) {
        if (!head || !head->next || !head->next->next) 
            return;
        //找中点,链表分成两个
        ListNode *s = head;
        ListNode *f = head;
        while (f->next && f->next->next) {
            s = s->next;
            f = f->next->next;
        }

        ListNode *newHead = s->next;
        s->next = NULL;

        //第二个链表倒置
        newHead = reverseList(newHead);

        //链表节点依次连接
        while (newHead) {
            ListNode* temp = newHead->next;
            newHead->next = head->next;
            head->next = newHead;
            head = newHead->next;
            newHead = temp;
        }

    }
    private:
    ListNode* reverseList(ListNode* head) {
        if (!head)
            return NULL;
        ListNode* tail = head;
        head = head->next;

        tail->next = NULL;

        while (head) {
            ListNode* temp = head->next;
            head->next = tail;
            tail = head;
            head = temp;
        }

        return tail;
    }


};
ListNode *createByTail()
{
    ListNode *head;
    ListNode *p1,*p2;
    int n=0,num;
    int len;
    cin>>len;
    head=NULL;
    while(n<len && cin>>num)
    {
        p1=new ListNode(num);
        n=n+1;
        if(n==1)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
    }
    return head;
}

void  displayLink(ListNode *head)
{
    ListNode *p;
    p=head;
    cout<<"head-->";
    while(p!= NULL)
    {
        cout<<p->val<<"-->";
        p=p->next;
    }
    cout<<"tail\n";
}
int main()
{
    ListNode* head = createByTail();
    Solution().reorderList(head);
    displayLink(head);
    return 0;
}

思想:将链表分两半,将第二个逆序,在依次连接

 

 

posted on 2020-09-04 15:53  Hi!Superman  阅读(260)  评论(0编辑  收藏  举报

导航