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

leetcode_25_Reverse Nodes in k-Group

时间:2015-02-16 11:45:12      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:linked_list   c++   leetcode   

欢迎大家阅读参考,如有错误或疑问请留言纠正,谢谢技术分享


Reverse Nodes in k-Grou

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


class Solution {
//once the link order of the list is changed, 
//we must keep the previous pointer(p in the code) updated, if we want to use it agian
public:
	void reverse_range(ListNode* prev, ListNode* end, ListNode*& p)
	{
		ListNode* last = prev->next;
		ListNode* cur = last->next;
		while(cur != end)
		{
			last->next = cur->next;
			cur->next = prev->next;
			prev->next = cur;

			cur = last->next;
		}
		p = last;
	}
	ListNode *reverseKGroup(ListNode *head, int k) 
	{
		if(k <= 1) return head;
		ListNode *dummy = new ListNode(-1);
		dummy->next = head;
		ListNode* prev = dummy;
		ListNode* p = dummy->next;
		int cnt = 1;
		while(p != NULL)
		{
			if(cnt%k == 0)
			{
				reverse_range(prev, p->next, p);//update p is very important
				prev = p;
			}
			p = p->next;
			cnt++;
		}
		return dummy->next;
	}
};


#include<iostream>

using namespace std;

#define N 5

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

class Solution {
//once the link order of the list is changed, 
//we must keep the previous pointer(p in the code) updated, if we want to use it agian
public:
	void reverse_range(ListNode* prev, ListNode* end, ListNode*& p)
	{
		ListNode* last = prev->next;
		ListNode* cur = last->next;
		while(cur != end)
		{
			last->next = cur->next;
			cur->next = prev->next;
			prev->next = cur;

			cur = last->next;
		}
		p = last;
	}
	ListNode *reverseKGroup(ListNode *head, int k) 
	{
		if(k <= 1) return head;
		ListNode *dummy = new ListNode(-1);
		dummy->next = head;
		ListNode* prev = dummy;
		ListNode* p = dummy->next;
		int cnt = 1;
		while(p != NULL)
		{
			if(cnt%k == 0)
			{
				reverse_range(prev, p->next, p);//update p is very important
				prev = p;
			}
			p = p->next;
			cnt++;
		}
		return dummy->next;
	}
};

ListNode *creatlist()
{
	ListNode *head = NULL;
	ListNode *p;
	for(int i=0; i<N; i++)
	{
		int a;
		cin>>a;
		p = (ListNode*) malloc(sizeof(ListNode));
		p->val = a;
		p->next = head;
		head = p;
	}
	return head;
}

int main()
{
	ListNode *list = creatlist();
	Solution lin;
	int a;
	cin>>a;
	ListNode *outlist = lin.reverseKGroup ( list,a );
	for(int i=0; i<N; i++)
	{
		cout<<outlist->val;
		outlist = outlist->next;
	}
}


leetcode_25_Reverse Nodes in k-Group

标签:linked_list   c++   leetcode   

原文地址:http://blog.csdn.net/keyyuanxin/article/details/43850901

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