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

Rotate List

时间:2014-12-21 16:35:20      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

class Solution {
public:
	typedef ListNode * SNode;
    ListNode *rotateRight(ListNode *head, int k) {
		SNode pcurrent , pnext,ptemp;
		if(k<=0 || head == NULL || head->next == NULL)
		return head;
		pnext    = head;
		pcurrent = head; 
		int count = 0;
		while(count <k)
		{
           
		   if(pnext->next != NULL)
		   {
			  pnext = pnext->next;
		   }
		   else
		   {
              pnext = head;
		   }
		   count++;
		}
		if(pcurrent == pnext)
		return head;
        while(pnext->next != NULL)
        {
            pcurrent = pcurrent->next;
			pnext    = pnext->next;

		}
		ptemp = pcurrent->next;
		pcurrent->next = NULL;
		pcurrent = ptemp;
		pnext->next = head;
		return pcurrent;
        
    }
};

 

Rotate List

标签:

原文地址:http://www.cnblogs.com/xgcode/p/4176654.html

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