leetcode_143_Reorder List,sortlistleetcode

來源:互聯網
上載者:User

leetcode_143_Reorder List,sortlistleetcode

歡迎大家閱讀參考,如有錯誤或疑問請留言糾正,謝謝


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==NULL)return;int len=getlistlen(head);ListNode *head1=NULL , *head2=NULL ;dividelist( head, len , head1 , head2 );head2 = reverseList(head2);        mergeList(head1, head2);    }private:int getlistlen(ListNode *head){int len=0;while(head){len++;head = head->next;}return len;}void dividelist(ListNode *head , int len , ListNode *&head1 , ListNode *&head2){int half = (len+1)/2;ListNode *first=head;head1 = head;for(int i=0; i<half-1; i++){first = first->next;}head2 = first->next;first->next = NULL;}ListNode* reverseList(ListNode* head)    {if( head == NULL)return NULL;ListNode *p = head->next;head->next = NULL;while(p){ListNode *temp = p;p = p->next;temp->next = head;head = temp;}return head;    }    void mergeList(ListNode* head1, ListNode* head2)    {        while(head2){ListNode *temp = head2;head2 = head2->next;temp->next = head1->next;head1->next = temp;head1 = head1->next->next;}    }};


#include<iostream>using namespace std;#define N 4struct ListNode {int val;ListNode *next;ListNode(int x) : val(x), next(NULL) {}};class Solution {public:    void reorderList(ListNode *head) {        if(head==NULL)return;int len=getlistlen(head);ListNode *head1=NULL , *head2=NULL ;dividelist( head, len , head1 , head2 );head2 = reverseList(head2);        mergeList(head1, head2);    }private:int getlistlen(ListNode *head){int len=0;while(head){len++;head = head->next;}return len;}void dividelist(ListNode *head , int len , ListNode *&head1 , ListNode *&head2){int half = (len+1)/2;ListNode *first=head;head1 = head;for(int i=0; i<half-1; i++){first = first->next;}head2 = first->next;first->next = NULL;}ListNode* reverseList(ListNode* head)    {if( head == NULL)return NULL;ListNode *p = head->next;head->next = NULL;while(p){ListNode *temp = p;p = p->next;temp->next = head;head = temp;}return head;    }    void mergeList(ListNode* head1, ListNode* head2)    {        while(head2){ListNode *temp = head2;head2 = head2->next;temp->next = head1->next;head1->next = temp;head1 = head1->next->next;}    }};ListNode *creatlist(){ListNode *head = NULL;ListNode *p;for(int i=0; i<N; i++){int a;cin>>a;p = (ListNode*) malloc(sizeof(ListNode));p->val = a;p->next = head;head = p;}return head;}int main(){ListNode *list = creatlist();Solution lin;lin.reorderList(list);for(int i=0; i<N; i++){cout<<list->val;list = list->next;}return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.