[LeetCode82]Rotate List
来源:互联网 发布:js实现一键关注公众号 编辑:程序博客网 时间:2024/04/30 00:39
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
.
Analysis
The idea is to get the whole length of the list, get the rotate position.
cut the list from the rotate position, and link the front part and back part.
c++
ListNode *rotateRight(ListNode *head, int k) { if(head == NULL || k == 0) return head; ListNode *p=head; int len = 1; while(p->next != NULL){ p = p->next; len++; } p->next = head; int dis = len - k%len; while(dis !=0){ p = p->next; dis--; } head = p->next; p->next = NULL; return head; }
java
public ListNode rotateRight(ListNode head, int n) {if(head == null || n==0) return head;ListNode p = head;int len = 1;while(p.next!=null){len++;p = p.next;}p.next = head;int dis = len - n%len;while(dis>0){p = p.next;dis--;}head = p.next;p.next = null; return head; }
0 0
- [LeetCode82]Rotate List
- 【leetcode82】Linked List Cycle
- LeetCode82:Remove Duplicates from Sorted List II
- leetcode82:Remove Duplicates from Sorted List II
- LeetCode82 Remove Duplicates from Sorted List II
- leetcode82. Remove Duplicates from Sorted List II
- LeetCode82.Remove Duplicates from Sorted List II
- leetcode82. Remove Duplicates from Sorted List II
- leetcode82~Remove Duplicates from Sorted List II
- LeetCode82 Remove Duplicates from Sorted List II
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- Rotate List
- tomcat配置
- ExtJs中Store的使用
- 黑马程序员 面向对象--->继承
- 使用apache的TelnetClient实现java向telnet服务器发送命令并显示数据(解决乱码问题)
- applet,欢迎学习java
- [LeetCode82]Rotate List
- Keytool用法
- 黑马程序员--线程一
- 软件工程-综合性实验
- cocos2d-x中的坐标系
- Maven学习笔记之二:Maven安装目录分析
- 测试工具
- 多进程编程实例
- 编写一个显示“欢迎学习Java"的Java Applet程序