Reverse Linked List以及一道有关倒序的题目

来源:互联网 发布:眼镜搭配脸型软件 编辑:程序博客网 时间:2024/05/16 09:43

一.Reverse a singly linked list.

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

递归法:

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* reverseList(ListNode* head) {      if(head&&head->next){      ListNode*p=head->next;      head->next=nullptr;      ListNode*newhead=reverseList(p);      p->next=head;      return newhead;      }      return head;            }};

每次把第一个节点扔到最后,循环到最终状态



迭代法:

(1) Create a Node named prehead before  head and  locate the immediate node before them-th (notice that it is1-indexed) node pre;

(2) Set cur to be the immediate node after pre and at each time move the immediate node aftercur (namedmove) to be the immediate node after pre. Repeat it forn - m times.

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* reverseList(ListNode* head) {        ListNode prehead(0),*pre=&prehead,*cur,*move;        pre->next=head;        cur=pre->next;        while(cur&&cur->next)        {            move=cur->next;            cur->next=move->next;            move->next=pre->next;            pre->next=move;        }        return prehead.next;            }};

二.

Reorder List

Given a singly linked list L: L0L1→…→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}.

这道题我先用了递归的方法,结果tle了 代码如下
class Solution {public:    void reorderList(ListNode* head) {        if(head!=nullptr&&head->next!=nullptr){        ListNode *p=head,*tail=head->next;        while(tail->next)        {            p=p->next;            tail=tail->next;        }        p->next=nullptr;        reorderList(head->next);        tail->next=head->next;        head->next=tail;        }    }};

然后我发现了我们可以把后半部分倒序,然后插入到前半部分,就可以完成这道题目
代码如下:
/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:       void reorderList(ListNode* head) {        if(head!=nullptr&&head->next!=nullptr&&head->next->next!=nullptr){        ListNode *pre,*tail,*cur,*move;        pre=head; tail=head;        int sum=0;        while(pre)        {            pre=pre->next;            sum++;        }        pre=head;        sum=sum/2;        for(int i=0;i<sum-1;i++)        pre=pre->next;        cur=pre->next;        while(cur->next)        {            move=cur->next;            cur->next=move->next;            move->next=pre->next;            pre->next=move;        }        pre->next=nullptr;        pre=head;                while(cur!=nullptr)        {            cur=pre->next;            pre->next=move;            pre=move;            move=cur;                                }                    }}};