#leetcode: the way to brush questions

Keywords: C++ less REST

A list is given. Each k nodes are flipped and the flipped list is returned.
k is a positive integer whose value is less than or equal to the length of the list. If the total number of nodes is not an integral multiple of k, the remaining nodes will remain in the original order.

Example:
Given this list: 1 - > 2 - > 3 - > 4 - > 5
When k = 2, it should return: 2 - > 1 - > 4 - > 3 - > 5
When k = 3, it should return: 3 - > 2 - > 1 - > 4 - > 5

Explain:
Your algorithm can only use constant extra space.
You can't just change the internal values of nodes, but you need to actually exchange nodes.

 

Idea: first, distinguish whether it is from the beginning, and then judge whether the quantity is enough before each turning

#include <iostream>
using namespace std;
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

ListNode* createlist(int n)//Generate linked list
{
    if (n == 0) return nullptr;
    ListNode* head = (ListNode*)malloc(sizeof(ListNode));
    cin >> head->val;
    ListNode* pre = head;
    for (int i = 0; i < n - 1; i++)
    {
        ListNode* p = (ListNode*)malloc(sizeof(ListNode));
        cin >> p->val;
        pre->next = p;
        pre = pre->next;
    }
    pre->next = nullptr;
    return head;
}

ListNode* reverselist(ListNode* head, ListNode *p, int k)
{
    int tk = k;//Temporary preservation k Value
    if (head == p)//Is the head
    {
        ListNode* temp = head;//
        ListNode* endd = head;//
        ListNode* ttemp=nullptr;
        ListNode* pre = nullptr;//Save the next reversed
        ListNode* end = nullptr;//
        ListNode* t = head;//Save the last reversed
        while (tk > 0)//Description from the beginning of the node K Nodes to reverse
        {
            if (temp != nullptr)
            {
                temp = temp->next;
                tk--;
            }
            else
                return head;
        }//while After cycle temp The pointer points to the K+1 Node
        //cout<<temp->val<<endl;
        //Start reversing, with the head pointing empty
        pre = head->next;
        head->next = nullptr;
        //cout<<temp->val<<endl;
        while (pre != temp)
        {
            //t = pre;
            end = pre->next;
            pre->next = head;
            head = pre;
            pre = end;
        }
        //cout << t->val << endl;
        endd->next = temp;//Connect the rest
        //Before taking the lead k We're done with it. We're going to judge if it's enough k individual
        tk = k;
        //temp = t;
        while (tk != 0)//Whether to continue processing
        {
            if (temp != nullptr)
            {
                temp = temp->next;
                tk--;
            }
            else
                return head;
        }
        //cout << t->val <<t->next->val<< endl;
        //cout << head->val << head->next->val << endl;
        reverselist(head, t, k);//If it's enough, it's recursive inversion
    }
    else
    {
        ListNode* pre = p;//Save the last reversed--------------------
        ListNode*cur = pre->next;//Save the first one to be reversed
        ListNode* cur_next=cur->next ;//Save next to reverse
        ListNode* end = nullptr;//Store the next node of the last node to be reversed----------------------
        ListNode* thead=nullptr;//Store the head node after transfer---------------
        ListNode* tcur = cur_next->next;//Store current cur_next Next node of,I.e. the one to reverse next time------------------
        ListNode* temp = nullptr;//Record the reverse end of the list
        ListNode* t = nullptr;//
        //-----------------------------Reverse first

        cur_next->next = cur;
        cur->next = nullptr;
        temp = cur;
        thead = cur_next;
        tk = k-2;
        while (tk > 0)
        {
            cur = tcur;
            tcur = tcur->next;
            cur->next = thead;
            thead = cur;
            tk--;
        }
        pre->next = thead;
        temp->next = tcur;
        t = temp->next;
        //Before taking the lead k We're done with it. We're going to judge if it's enough k individual
        tk = k;
        //temp = t;
        while (tk != 0)//Whether to continue processing
        {
            if (t != nullptr)
            {
                t = t->next;
                tk--;
            }
            else
                return head;
        }
        reverselist(head, temp, k);//If it's enough, it's recursive inversion
    }
    return head;
}

ListNode* reverseKGroup(ListNode* head, int k) {
    if (head == nullptr || k == 0 || k == 1) return head;
    ListNode *temp = head;
    head = reverselist(head, head, k);
    return  head;
}

int main() {
    ListNode* head = createlist(4);
    ListNode*ans = reverseKGroup(head, 2);
    while (ans != nullptr)
    {
        cout << ans->val << endl;
        ans = ans->next;
    }
    return 0;
}

Posted by Spekta on Thu, 05 Dec 2019 14:03:57 -0800