您的位置:首页 > Web前端 > Node.js

LeetCode 25:Reverse Nodes in k-Group

2015-11-28 00:48 736 查看
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


Subscribe to see which companies asked this question
方法一:利用栈结构

//利用一个栈来实现每k个结点的逆序,
//每组逆序结束后,将每组最后一个结点与下一组第一个结点重新链接
//例如:对单链表1->2->3->4->5->6->7->8->9进行每4个结点一组进行逆序
class Solution{
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if (head == NULL || head->next == NULL || k < 2)
return head;

stack<ListNode*> stack1;
ListNode *newHead = head;
ListNode *cur = head;
ListNode *pre = NULL;
ListNode *next = NULL;
while (cur != NULL)
{
next = cur->next;
stack1.push(cur);   //将结点依次入栈
if (stack1.size() == k)  //当入栈结点达到k个时,这k个结点开始逆序
{
pre = resign1(stack1, pre, next);  //pre=cur1 第一组结点逆序后,pre指向结点1
while (!stack1.empty())    //每组逆序后,将stack清空
{
stack1.pop();
}
newHead = newHead == head ? cur : newHead;  //记录新的头结点,第一组全部入栈后,cur指向结点4,
//第一组结点逆序后,新头结点变为4
}
cur = next;   //第一组逆序结束后,cur重新指向下一组的第一个结点5,开始遍历下一组
}
return newHead;
}
//将k个结点按照出栈顺序依次重新链接(比如:原单链表1->2->3->4,出栈后变成4->3->2->1)
ListNode * resign1(stack<ListNode*>stack2, ListNode* left, ListNode* right){
ListNode *cur1 = stack2.top();
if (left != NULL)
{
left->next = cur1;
}
ListNode *next = NULL;
while (!stack2.empty())
{
next = stack2.top( );
cur1->next = next;     //依次重新链接
cur1 = next;
stack2.pop();
}
cur1->next = right;  //1->2->3->4,出栈后变成4->3->2->1后,cur1指向结点1,重新链接上结点5
//1->2->3->4->5->6->7->8->9变为4->3->2->1->5->6->7->8->9
return cur1;
}
};


完整测试代码:

#include <iostream>
#include<stack>
#include<list>
using namespace std;

struct ListNode{
int data;
ListNode *next;
ListNode(int x) :data(x), next(NULL){}
};

//利用一个栈来实现每k个结点的逆序,
//每组逆序结束后,将每组最后一个结点与下一组第一个结点重新链接
class Solution{
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if (head == NULL || head->next == NULL || k < 2)
return head;

stack<ListNode*> stack1;
ListNode *newHead = head;
ListNode *cur = head;
ListNode *pre = NULL;
ListNode *next = NULL;
while (cur != NULL)
{
next = cur->next;
stack1.push(cur);   //将结点依次入栈
if (stack1.size() == k)  //当入栈结点达到k个时,这k个结点开始逆序
{
pre = resign1(stack1, pre, next);  //pre=cur1 第一组结点逆序后,pre指向结点1

while (!stack1.empty())    //每组逆序后,将stack清空
{
stack1.pop();
}

newHead = newHead == head ? cur : newHead;  //记录新的头结点,第一组全部入栈后,cur指向结点4,
//第一组结点逆序后,新头结点变为4
}
cur = next;   //第一组逆序结束后,cur重新指向下一组的第一个结点5,开始遍历下一组
}
return newHead;
}

//将k个结点按照出栈顺序依次重新链接(比如:原单链表1->2->3->4,出栈后变成4->3->2->1)
ListNode * resign1(stack<ListNode*>stack2, ListNode* left, ListNode* right){
ListNode *cur1 = stack2.top();
if (left != NULL)
{
left->next = cur1;
}
ListNode *next = NULL;
while (!stack2.empty())
{
next = stack2.top( );
cur1->next = next;     //依次重新链接
cur1 = next;
stack2.pop();
}
cur1->next = right;  //1->2->3->4,出栈后变成4->3->2->1后,cur1指向结点1,重新链接上结点5
//1->2->3->4->5->6->7->8->9变为4->3->2->1->5->6->7->8->9
return cur1;
}
};

//创建只有空链表,返回一个dummy节点
ListNode * createList()
{
ListNode *dummy = new ListNode(-1);
return dummy;
}

ListNode * initList(ListNode *dummy)
{
//用一个数组初始化链表
int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

ListNode *tmp, *head;
tmp = dummy;
//使用的是尾插入法
for (int i = 0; i<sizeof(array) / sizeof(int); i++)
{
ListNode *p = new ListNode(array[i]);
tmp->next = p;
tmp = tmp->next;
}
head = dummy->next;
delete dummy;
return head;
}

//显示链表
void showList(ListNode *head)
{
while (head)
{
printf("%d ", head->data);
head = head->next;
}
printf("\n");
return;
}

//摧毁链表
void desroyList(ListNode *head)
{
while (head)
{
ListNode *tmp = head->next;
delete head;
head = tmp;
}
return;
}

void main()
{
Solution sol1;
//创建一个空链表
ListNode *dummy = createList();
//对链表初始化,返回head
ListNode *head = initList(dummy);
//显示链表
showList(head);
//swap
head = sol1.reverseKGroup(head, 4);
//显示链表
showList(head);
system("pause");
desroyList(head);
}


方法二:不利用栈结构,直接在原链表中调整

//不利用栈结构,在原链表中直接调整
class Solution{
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if (head == NULL || head->next == NULL || k < 2)
return head;

ListNode *cur = head;
ListNode *start = NULL;
ListNode *pre = NULL;
ListNode *next = NULL;

int count = 1;
while (cur != NULL)
{
next = cur->next;

if (count == k)
{
start = pre == NULL ? head : pre->next;
head = pre == NULL ? cur : head;
resign2(pre, start, cur, next);
pre = start;
count = 0;
}
count++;
cur = next;
}
return head;
}

void resign2(ListNode* left, ListNode* start, ListNode *end,ListNode* right){
ListNode *pre = start;
ListNode* cur = start->next;
ListNode*next = NULL;

while (cur!=right)
{
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
if (left != NULL){
left->next = end;
}
start->next = right; //上一组的最后一个结点(逆序前的第一个结点)链接下一组逆序前的第一个结点
}
};


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: