每日一题 2019 - 04 - 04
题目:
Given a linked list, rotate the list to the right by k places, where k is non-negative.
Example 1:
1 | Input: 1->2->3->4->5->NULL, k = 2 |
Example 2:
1 | Input: 0->1->2->NULL, k = 4 |
解法:
这个题让我们将链表中的元素往前推 K 位,其实就是等于将数组中的元素往后移动,思路很简单,创立中间 vector,将 vector 移动完成后,重新赋值给链表;
不过要注意一点,移动的步数 K 一定要记得对数组的长度取模,不然就是超时!
代码:
1 | /** |