C++ K个一组反转链表

Posted fulianzhou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ K个一组反转链表相关的知识,希望对你有一定的参考价值。

https://leetcode.cn/problems/reverse-nodes-in-k-group/

// 25-K-reverse.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

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 addNode(ListNode* &head, int val)
    
        if (!head)
        
            head = new ListNode(val);
            return;
        

        ListNode* pNode = new ListNode(val);

        pNode->next = head;
        head = pNode;
    

    void traver(ListNode* head)
    
        while (head)
        
            printf("%d ", head->val);

            head = head->next;
        
        printf("\\n");
    

    ListNode* reverseKGroup(ListNode* head, int k) 
    
        if (k <= 1)
        
            return head;
        

        int n = 1;
        ListNode* root = head;
        ListNode* pStart = head;
        ListNode* pPrev = NULL;
        while (head)
        
            if (n % k == 0)
            
                // ->[9]->[8]->[7]->[6]->[5]->[4]->[3]->[2]->[1]->[0]
                // ->[9]<-[8]<-[7]  [6]->[5]->[4]->[3]->[2]->[1]->[0]
                //  |               ↑
                //  |---------------|

                // ->[7]->[8]->[9]->[6]->[5]->[4]->[3]->[2]->[1]->[0]
                // ->[7]->[8]->[9]-------------|  [3]->[2]->[1]->[0]
                //                           ↓   ↑
                //                [6]<-[5]<-[4]  |
                //                 |             |
                //                 |-------------|
                // [7]->[8]->[9]->[4]->[5]->[6]->[3]->[2]->[1]->[0]
                // [7]->[8]->[9]->[4]->[5]->[6]->[1]->[2]->[3]->[0]
                ListNode* pEndNext = head->next;
                ListNode* pCurr = pStart->next;
                ListNode* pLast = pStart;

                do 
                
                    ListNode* tmp = pCurr;
                    pCurr = pCurr->next;

                    tmp->next = pLast;

                    pLast = tmp;
                 while (pCurr != pEndNext);

                if (pPrev)
                
                    pPrev->next = head;
                
                else
                
                    root = pLast;
                
                pStart->next = pEndNext;

                pPrev = pStart;
                pStart = pEndNext;

                head = pEndNext;
            
            else
            
                head = head->next;
            
            ++n;
        

        return root;
    
;


int _tmain(int argc, _TCHAR* argv[])

    Solution sol;

    ListNode* head1 = NULL;
    ListNode* head2 = NULL;
    ListNode* head3 = NULL;
    ListNode* head4 = NULL;
    for (int i = 10; i > 0; i--)
    
        sol.addNode(head1, i);
        sol.addNode(head2, i);
        sol.addNode(head3, i);
        sol.addNode(head4, i);
    

    sol.traver(head1);
    head1 = sol.reverseKGroup(head1, 2);
    sol.traver(head1);

    head2 = sol.reverseKGroup(head2, 3);
    sol.traver(head2);

    head3 = sol.reverseKGroup(head3, 4);
    sol.traver(head3);

    head4 = sol.reverseKGroup(head4, 5);
    sol.traver(head4);

	return 0;



以上是关于C++ K个一组反转链表的主要内容,如果未能解决你的问题,请参考以下文章

***25 k个一组反转链表

链表--K个一组反转链表(leetcode 25

Leetcode25. K 个一组翻转链表(头插法反转)

LeetCode 第25题: k个一组翻转链表

25. k个一组翻转链表-LeetCode

链表题--01----K 个一组翻转链表