码迷,mamicode.com
首页 > 其他好文 > 详细

25. Reverse Nodes in k-Group

时间:2017-09-23 10:40:17      阅读:94      评论:0      收藏:0      [点我收藏+]

标签:reverse   next   des   span   log   while   solution   nbsp   div   

class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if(head==null||k==1)
            return head;
        ListNode preNode=new ListNode(0);
        preNode.next=head;
        ListNode pre=preNode, cur=pre, nex;
        int num=0;
        while(cur.next!=null)
        {
            num++;
            cur=cur.next;
        }
        while(num>=k)
        {
            cur=pre.next;
            nex=cur.next;
            for(int i=1;i<k;i++)
            {
                cur.next=nex.next;
                nex.next=pre.next;
                pre.next=nex;
                nex=cur.next;
            }
            pre=cur;
            num-=k;
        }
        return preNode.next;
    }
}

 

25. Reverse Nodes in k-Group

标签:reverse   next   des   span   log   while   solution   nbsp   div   

原文地址:http://www.cnblogs.com/asuran/p/7580055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!