[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.

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

题意

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

题解

http://blog.csdn.net/u014654002/article/details/51538495

/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
{
for(int i=1;i<k&&end;i++)
end=end->next;
if(end==NULL) break;
prev=reverse(prev,prev->next,end);
}
}
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 K区间链表交换 解题报告

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

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

• 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 list. ...

leetcode25---Reverse Nodes in k-Group

举报原因： 您举报文章：深度学习：神经网络中的前向传播和反向传播算法推导 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)