LeetCode: Reverse Nodes in k-Group
2015-07-18 20:58
531 查看
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:
For k = 2, you should return:
For k = 3, you should return:
思路:用一个指针记录已经转置的链表的尾节点。
从头节点开始,设置一个指针对其移动k个,将其断开,调用原地转置链表的代码。转置链表插入到已经转置好的链表的尾部。如果最后剩下不足k个,直接将剩余链表插入到尾部
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
//原地转置链表函数
ListNode* reverseList(ListNode* head)
{
if(head == nullptr) return head;
ListNode *p = head;
ListNode *q = head->next;
head->next = nullptr;
while(q != nullptr)
{
ListNode *t = q->next;
q->next = p;
p = q;
q = t;
}
return p;
}
ListNode* reverseKGroup(ListNode* head, int k) {
//头节点为空或者分组为1可以直接返回
if(head == nullptr || k==1) return head;
//用于记录头节点和尾节点
ListNode* nhead = nullptr;
ListNode* ntail = nullptr;
//需要转置链表的头 p和尾 q
ListNode* p = head;
ListNode* q = p;
while(q!=nullptr)
{
//找到长度为k的链表
for(int step = 0; q != nullptr && step < (k-1); ++ step) q = q->next;
if(q == nullptr)
{
//如果没找到直接插入到尾部
if(nhead == nullptr) nhead =head;
else ntail->next = p;
break;
}
else
{
//找到了记录下一节点,并分割出一个链表进行转置
ListNode* t = q->next;
q->next = nullptr;
reverseList(p);
//将转置链表插入尾部,之前的头p成了尾,之前的尾q成为头。设置新的尾部为p
if(nhead == nullptr)
{
ntail = p;
nhead = q;
}
else
{
ntail->next = q;
ntail = p;
}
q = t;
p = t;
}
}
return nhead;
}
};
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个,直接将剩余链表插入到尾部
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
//原地转置链表函数
ListNode* reverseList(ListNode* head)
{
if(head == nullptr) return head;
ListNode *p = head;
ListNode *q = head->next;
head->next = nullptr;
while(q != nullptr)
{
ListNode *t = q->next;
q->next = p;
p = q;
q = t;
}
return p;
}
ListNode* reverseKGroup(ListNode* head, int k) {
//头节点为空或者分组为1可以直接返回
if(head == nullptr || k==1) return head;
//用于记录头节点和尾节点
ListNode* nhead = nullptr;
ListNode* ntail = nullptr;
//需要转置链表的头 p和尾 q
ListNode* p = head;
ListNode* q = p;
while(q!=nullptr)
{
//找到长度为k的链表
for(int step = 0; q != nullptr && step < (k-1); ++ step) q = q->next;
if(q == nullptr)
{
//如果没找到直接插入到尾部
if(nhead == nullptr) nhead =head;
else ntail->next = p;
break;
}
else
{
//找到了记录下一节点,并分割出一个链表进行转置
ListNode* t = q->next;
q->next = nullptr;
reverseList(p);
//将转置链表插入尾部,之前的头p成了尾,之前的尾q成为头。设置新的尾部为p
if(nhead == nullptr)
{
ntail = p;
nhead = q;
}
else
{
ntail->next = q;
ntail = p;
}
q = t;
p = t;
}
}
return nhead;
}
};
相关文章推荐
- Delete Node in a Linked List
- 237-e-Delete Node in a Linked List
- leetcode | Reverse Nodes in k-Group
- Node.js开发环境配置
- 学习老外用webstorm开发nodejs的技巧--代码提示DefinitelyTyped
- Node 实现 AES 加密,结果输出为“byte”。
- Delete Node in a Linked List
- Remove Nth Node From End of List
- [LeetCode]Delete Node in a Linked List
- leetcode | Swap Nodes in Pairs in a linklist
- ubuntu server 14.10 安装 nodejs
- Delete Node in a Linked List
- Node实践之二
- Windows环境下的nodejs环境开发搭建
- 在hadoop2实际生产环境中,为什么还需要SecondeNamenode
- Delete Node in a Linked List
- datanode无法启动 或 DFS Used% :100 % 或 Live Nodes
- Populating Next Right Pointers in Each Node
- 【node.js】安装express后,'express' 不是内部或外部命令的问题
- nodejs 调度 node-schedule 使用