Leetcode-Rotate List
来源:互联网 发布:centos u盘挂载 编辑:程序博客网 时间:2024/06/02 05:59
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,
For example:
Given 1->2->3->4->5->NULL and k = 2,
return 4->5->1->2->3->NULL.
题目大意:
给出一个单链表,和一个K值,根据K值往右旋转,例如:
先求出链表的长度length,其实按着倒数第n%length个位置旋转。
AC代码:
public class Solution {// public ListNode reverseList( ListNode head ){// if( head == null || head.next == null )// return head;// ListNode p = head;// ListNode q = head;// while( q != null ){// ListNode temp = q.next;// q.next = p;// p = q;// q = temp;// }// head.next = null;// head = p;// return head;// }public ListNode rotateRight(ListNode head, int n){if( head == null || head.next == null || n<=0 )return head;ListNode before = head;ListNode after = head;int length = 0;while( before != null ){length++;before = before.next;}before = head;//if( n > length ) return reverseList(head); if( n%length == 0 ) return head;for( int i=0; i<(n%length); i++ ){after = after.next;}if( after == null ) return head;while( after.next != null ){before = before.next;after = after.next;}ListNode temp = before.next;before.next = null;after.next = head;head = temp;return head;}}
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
- LeetCode 题解(48): Minimum Path Sum
- MySql入门命令总结
- 【Information Retrieval】文本信息检索
- python Tkinter (1)
- ARM和NEON指令
- Leetcode-Rotate List
- hdu 5007 Post Robot
- UVA - 10718 Bit Mask(贪心+位运算)
- 并查集入门
- Netty那点事(三)Channel中的Pipeline
- 数据结构笔记(1)
- 【Linux】【Shell】Linux/Unix下Shell快捷键操作大集合
- 火影600集最新片头曲《シルエット》歌词
- 段很长的道路,一个油漆工刷路面,他有两种工具,工具1每次可以刷1米,工具2每次可以刷3米,1)问当道路长度是n米时,使用工具1和工具2配合使用刷完全部道路,共有多少种方式?