leetcode143. 重排链表

class Solution {
public:
    void reorderList(ListNode* head) {
        if(!head)return ;
        vector<ListNode*>v;
        ListNode*cur=head;
        while(cur){
            v.push_back(cur);
            cur=cur->next;
        }
        int i=0,j=v.size()-1;
        while(i<j){
            v[i]->next=v[j];
            i++;
            if(i==j)break;
            v[j]->next=v[i];
            j--;
        }
        v[i]->next=nullptr;
    }
};
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        //找到中点slow
        ListNode *fast=head,*slow=head;
        while(fast){
            if(!fast->next)break;
            fast=fast->next->next;
            slow=slow->next;
        }
        //旋转链表
        ListNode *dummy=new ListNode();
        ListNode *cur=slow;
        while(cur){
            ListNode*temp=cur;
            cur=cur->next;
            temp->next=dummy->next;
            dummy->next=temp;
        }
        //重排链表
        cur=dummy->next;
        fast=head;
        ListNode *ans=new ListNode();
        ListNode *tail=ans;
        while(fast!=slow&&cur){
            tail->next=fast;
            tail=tail->next;
            fast=fast->next;
            tail->next=cur;
            tail=tail->next;
            cur=cur->next;
        }
        
    }
};

官解

class Solution {
public:
    void reorderList(ListNode* head) {
        if (head == nullptr) {
            return;
        }
        ListNode* mid = middleNode(head);
        ListNode* l1 = head;
        ListNode* l2 = mid->next;
        mid->next = nullptr;
        l2 = reverseList(l2);
        mergeList(l1, l2);
    }

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

    ListNode* reverseList(ListNode* head) {
        ListNode* prev = nullptr;
        ListNode* curr = head;
        while (curr != nullptr) {
            ListNode* nextTemp = curr->next;
            curr->next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;
    }

    void mergeList(ListNode* l1, ListNode* l2) {
        ListNode* l1_tmp;
        ListNode* l2_tmp;
        while (l1 != nullptr && l2 != nullptr) {
            l1_tmp = l1->next;
            l2_tmp = l2->next;

            l1->next = l2;
            l1 = l1_tmp;

            l2->next = l1;
            l2 = l2_tmp;
        }
    }
};

作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/reorder-list/solution/zhong-pai-lian-biao-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。