Rotate List
来源:互联网 发布:go python 编辑:程序博客网 时间:2024/06/08 04:22
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.
注意K可能比总长度大,所以移动k%length
ListNode *rotateRight(ListNode *head, int k){ if (head == NULL || head->next == NULL || k == 0) return head; int length = 0; ListNode *p = head, *old_tail = head; while (p != NULL) { length++; old_tail = p; p = p->next; } k %= length; ListNode *new_tail = head, *new_head = head; for (int i = 0; i < length - k - 1; i++) { new_tail = new_tail-> next; } old_tail->next = head; new_head = new_tail->next; new_tail->next = NULL; return new_head;}
0 0
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- rotate-list
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Oracle Sql Loader的学习使用
- 应用程序清单
- epoll与select、poll区别
- java类加载器和动态代理
- 关于编译源码时出现Syntax error: "(" unexpected的解决方式
- Rotate List
- ubuntu13.10 安装sublime Text 2.0.2 以及破解过程
- 转:vxWorks 653的x86 arch 代码移植
- 教你不用编程,设计一个Android蓝牙遥控小车软件
- D-Bus学习(四):基础小例子(同步和异步)
- HttpClient之源码导入MyEclipse
- java垃圾收集相关内容
- App Manifest 文档翻译
- poj 1204 Word Puzzles