zoukankan      html  css  js  c++  java
  • Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equal to the length of the linked list. If the number of

    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;  
        }  
    };
    

      

  • 相关阅读:
    内联汇编的使用
    metamask注记词
    一个简单的增删改查合约
    企业邮箱账号
    压缩包管理
    设计模式
    软硬链接ln
    文件IO-Linux
    静态库和动态库
    gdb调试
  • 原文地址:https://www.cnblogs.com/Czc963239044/p/7103235.html
Copyright © 2011-2022 走看看