Leetcode解题-链表(2.2.6)RotateList

题目: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是否越界。最终旋转时注意不要形成循环链表,导致打印链表时死循环!


 

原文地址:https://www.cnblogs.com/xiaomaohai/p/6157638.html