[leetcode] 【链表】25. Reverse Nodes in k-Group

原创 2016年06月02日 00:31:21

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

If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5


题意

一个链表,每k个节点为一部分,然后这k个节点逆序翻转,如果最后剩余不足k个节点,那么保持原来的顺序,

Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5


题解

从头开始遍历,每到k个便翻转,不够k个就跳出。
翻转方法参考92题
92. Reverse Linked List II
http://blog.csdn.net/u014654002/article/details/51538495

/**
 * 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(head==NULL||head->next==NULL||k<2) return head;
        ListNode newhead(-1);
        newhead.next=head;
        for(ListNode *prev=&newhead,*end=head;end;end=prev->next)
        {
            for(int i=1;i<k&&end;i++)
                end=end->next;
            if(end==NULL) break;
            prev=reverse(prev,prev->next,end);
        }
        return newhead.next;
    }
    ListNode* reverse(ListNode *prev,ListNode *begin,ListNode *end)
    {
        ListNode* temp=end->next;
        for(ListNode *p=begin,*cur=p->next,*next=cur->next;cur!=temp;
        p=cur,cur=next,next=next? next->next:NULL)
            cur->next=p;
        begin->next=temp;
        prev->next=end;
        return begin;
    }
};




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode (25) Reverse Nodes in k-Group (链表)

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

LeetCode 25. Reverse Nodes in k-Group(反转链表)

原题网址:https://leetcode.com/problems/reverse-nodes-in-k-group/ Given a linked list, reverse the node...

leetcode 25. Reverse Nodes in k-Group——很值得一看的链表题

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

Leetcode #25 Reverse Nodes in k-Group K区间链表交换 解题报告

1 解题思想昨天在写组内交换的时候我就说了,肯定有K组版的,结果就是今天啊。。嗯,因为我写的和做的不同步,所以已经不记得题目顺序了。那么在看今天的之前,可以先去回顾下昨天的,今天的是昨天的推广版,昨天...

LeetCode 25 Reverse Nodes in k-Group(在K组链表中反转结点)(Linked List)(*)

原文给定一个链表,在一定时间内反转这个链表的结点,并返回修改后的链表。如果结点数不是K的倍数,那么剩余的结点就保持原样。你不应该在结点上修改它的值,只有结点自身可以修改。只允许使用常量空间。例如给定链...
  • NoMasp
  • NoMasp
  • 2015-11-13 11:11
  • 2182

leetCode 25.Reverse Nodes in k-Group (以k个节点为一组反转链表) 解题思路和方法

Reverse Nodes in k-Group  Given a linked list, reverse the nodes of a linked list k at a time and re...

25. Reverse Nodes in k-Group 怎样不使用额外空间翻转一个单向链表?

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

leetcode:Reverse Nodes in k-Group(以k为循环节反转链表)【面试算法题】

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

[leetcode]Reverse Nodes in k-Group 反转以k个节点为一组的链表

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

leetcode25---Reverse Nodes in k-Group

问题描述: Given a linked list, reverse the nodes of a linked list k at a time and return its modified l...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)