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

Leetcode解题-链表(2.2.6)RotateList

时间:2015-03-28 11:33:32      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:leetcode   algorithm   

题目:Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative.

For example:

Given 1->2->3->4->5->nullptr and k = 2, return 4->5->1->2->3->nullptr.

实现

首先确定题目要求的旋转都需要哪些结点:dummy头结点、结点K、尾结点,然后再去定位。注意,题目中提到前置条件k是非负数,所以要判断k是否越界。最终旋转时注意不要形成循环链表,导致打印链表时死循环!

技术分享

 

Leetcode解题-链表(2.2.6)RotateList

标签:leetcode   algorithm   

原文地址:http://blog.csdn.net/dc_726/article/details/44699867

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