[LeetCode] C++: Intermediate Problem-Linked List 143. Rearrange Linked List

143. Rearrange linked lists

Medium difficulty 511

Given a singly linked list  L : L 0→ L 1→…→ L n -1→ L n,
rearrange it into:  L 0→ L nL 1→ L n -1→ L 2→ L n -2→…

You can't just simply change the internal value of the node, but need to actually exchange the node.

Example 1:

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

Example 2:

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

1. The linked list array is stored in the linked list, and then rearranged using the index of the array

/**
 * 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) {
        if(head == nullptr){
            return ;
        }
        vector<ListNode*> vec;
        ListNode* node = head;
        while(node != nullptr){
            vec.push_back(node);
            node = node->next;
        }
        int i = 0, j = vec.size()-1;
        while(i < j){
            vec[i]->next = vec[j];
            i++;
            if(i == j){
                break;
            }
            vec[j]->next = vec[i];
            j--;
        }
        vec[i]->next = nullptr;
    }
};

2. Find the midpoint-reverse-merge

The general idea is this:

First find the midpoint of the linked list, and then reverse all the nodes after the midpoint. Finally, the process of merging is more complicated and requires careful drawing on paper to find the rules. In short, I learned a lot from the official solution.

/**
 * 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) {
        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* tmp = curr->next;
            curr->next = prev;
            prev = curr;
            curr = tmp;
        }
        return prev;
    }

    void mergeList(ListNode* l1, ListNode* l2){
        ListNode* l1_tmp, *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;
        }
    }
};
/**
 * 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) {
        if(head == nullptr || head->next == nullptr || head->next->next == nullptr){
            return;
        }
        ListNode *slow = head, *fast = head->next;
        while(fast != nullptr && fast->next != nullptr){
            slow = slow->next;
            fast = fast->next->next;
        }   //找中点

        //2 断开中点后,反转后半部分
        ListNode *head2 = nullptr, *nxt = slow->next;
        slow->next = nullptr;
        slow = nxt;
        while(slow != nullptr){
            nxt = slow->next;
            slow->next = head2;
            head2 = slow;
            slow = nxt;
        }

        //3 合并链表
        ListNode* curr = head, *curr2 = head2;
        while(curr != nullptr && curr2 != nullptr){
            nxt = curr->next;
            curr->next = curr2;
            curr2 = curr2->next;
            curr->next->next = nxt;
            curr = nxt;
        }

    }
};

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/113574457