首页 > 其他 > 详细

Leetcode 0025. Reverse Nodes in k-Group

时间:2016-09-15 01:03:05      阅读:201      评论:0      收藏:0      [点我收藏+]
技术分享
居然把头插法写错了,debug了一个多小时
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        if(k == 1 || head == nullptr)  return head;
        ListNode *tail = head;
        int i;
        for(i=0;i<k && tail != nullptr;i++,tail=tail->next);
        if(i<k) return head;
        tail = reverseKGroup(tail, k);
        ListNode* rear = head,*tmp=nullptr;
        head= tail;
        for(int i = 0;i<k;i++){
              tmp = rear->next;        
              rear->next = head;
              head = rear;
              rear = tmp;
        }
        return head;
    }
};
View Code

 

Leetcode 0025. Reverse Nodes in k-Group

原文:http://www.cnblogs.com/zeroArn/p/5874053.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!