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

思路:

每k个元素为一个group,翻转,然后串联起来。如果不够k个元素,则维持不变。

#include <iostream>
using namespace std;

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

//每k个元素为一个group,翻转,然后串联起来。如果不够k个元素,则维持不变。
//方法1:
LinkNode * reverseNodesInKGroup(LinkNode *head,int k)
{
	LinkNode *dummy = new LinkNode(-1);
	dummy->next = head;
	LinkNode *tmp,*pre,*cur,*post,*begin,*end;	
	pre=dummy;//指向group的前驱
	cur = head;
	post = head->next;

	while(cur)
	{
		//找begin
		begin = cur;

		//找到第K个位置.如果找不到k个位置,那么end就会是null
		end = cur;
		for(int i=1;i<k&&end!=NULL;i++)
		{
			end = end->next;
		}		

		if(end)
		{
			pre->next = end;//与上一个group串联

			//翻转
			post = cur->next;
			for(int i=1;i<k;i++)
			{
				tmp = post->next;
				post->next = cur;
				cur = post;
				post = tmp;
			}
			cur = post;//cur指向下一个group的首节点

			pre = begin;//翻转后begin位于最后一个节点,作为下一个group的前驱
			if(!cur)
				pre->next = NULL;
		}
		else
		{
			//啥也不用做,保持原样
			pre->next = begin;
			break;
		}		
	}

	head = dummy->next;
	delete dummy;
	return head;
}

//改进,方法2
LinkNode *reverseNodesInKGroup2(LinkNode *head,int k)
{
	LinkNode *dummy = new LinkNode(-1);
	dummy->next = head;
	LinkNode *pre = dummy;

	while(pre->next)
	{
		LinkNode *last = pre->next;
		//找到第K个元素,看看是否为NULL
		for(int i=1;i<k&&last;i++)//这里要判断Last是否为空,否则会出现空指针异常
			last=last->next;

		//如果第K个元素存在
		if(last)
		{
			LinkNode *p = last->next;
			LinkNode *first = pre->next;
			//group内逆序
			LinkNode *cur = pre->next;
			LinkNode *post = cur->next;
			for(int i=1;i<k;i++)
			{
				LinkNode *tmp = post->next;
				post->next = cur;
				cur=post;
				post=tmp;
			}			
			pre->next = last;  //pre链接上最后一个
			first->next = p;//first链接上后一个group			
			pre = first;//pre挪到first,作为下一个group的pre
		}
		else
		{
			//什么也不做,保持原来的顺序
			break;
		}
	}
	head = dummy->next;
	delete dummy;
	return head;
}

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

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

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

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

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

	void main()
	{
		//创建一个空链表
		LinkNode *dummy = createLink();
		//对链表初始化,返回head
		LinkNode *head = initLink(dummy);
		//显示链表
		showList(head);
		//swap
		head = reverseNodesInKGroup2(head,4);
		//显示链表
		showList(head);
		desroyList(head);
	}



你可能感兴趣的:(Reverse Nodes in k-Group)