LeetCode-Rotate List
来源:互联网 发布:gif动态图片编辑软件 编辑:程序博客网 时间:2024/04/20 13:19
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->NULL
and k = 2
,
return 4->5->1->2->3->NULL
.
Code:
<span style="font-size:14px;">/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *rotateRight(ListNode *head, int k) { if (head == NULL || head->next == NULL) return head; int length = 0; ListNode *copy = head, *temp; while (copy != NULL) { length++; copy = copy->next; } k = k%length; if (k == 0) return head; copy = head; for (int i = 0; i < length-k-1; ++i) copy = copy->next; temp = copy->next; copy->next = NULL; copy = temp; while (copy != NULL && copy->next != NULL) copy = copy->next; copy->next = head; return temp; }};</span>
0 0
- [leetcode][list] Rotate List
- LeetCode: Rotate List
- LeetCode Rotate List
- LeetCode : Rotate List
- [Leetcode] Rotate List
- [LeetCode] Rotate List
- [leetcode] Rotate List
- leetcode 70: Rotate List
- [Leetcode] Rotate List
- 【leetcode】Rotate List
- [LeetCode]Rotate List
- [leetcode]Rotate List
- LeetCode-Rotate List
- [leetcode] Rotate List
- LeetCode - Rotate List
- LeetCode:Rotate List
- LeetCode 61: Rotate List
- leetcode之Rotate List
- 用python给文件夹下所有图片进行缩放处理
- IE6兼容性总汇
- LWIP程序OPT.h头文件的各种定义
- hdu1559,1081最大子矩阵和的两种题型
- ftp批量下载 单个下载
- LeetCode-Rotate List
- struts2 action重定向action
- 学习pthreads,使用互斥量进行同步
- C#建立VPN
- 正则表达式基础及java使用
- C语言中typedef的用法说明(最好还…
- c语言心得
- 2014年05月11日
- 动态映射objective-c的对象方法修改空指针