Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k

Posted 洞拐洞幺

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k相关的知识,希望对你有一定的参考价值。

class Solution {  
public:  
    ListNode *reverseKGroup(ListNode *head, int k) {  
        if (!head || !(head->next) || k < 2)  
            return head;  
          
        // count k nodes  
        ListNode *nextgp = head;  
        for (int i = 0; i < k; i++)  
            if (nextgp)  
                nextgp = nextgp->next;  
            else  
                return head;  
  
        // reverse  
        ListNode *prev = NULL, *cur = head, *next = NULL;  
        while (cur != nextgp) {  
            next = cur->next;  
            if (prev)  
                cur->next = prev;  
            else  
                cur->next = reverseKGroup(nextgp, k);  
            prev = cur;  
            cur = next;  
        }  
        return prev;  
    }  
};

  

以上是关于Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k的主要内容,如果未能解决你的问题,请参考以下文章

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k

Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.

A linked list is given such that each node contains an additional random pointer which could point t

problem report: middle of linked list

Reverse Linked List II

92. NoReverse Linked List 2