LeetCode——Rotate List

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.

原题链接:https://oj.leetcode.com/problems/rotate-list/

得到链表长度,链表指向尾节点,将链表首尾相连,向右k%len,得到结果链表的尾节点。

public class RotateList {
	public static void main(String[] args) {
		ListNode head = new ListNode(1);
		ListNode head1 = new ListNode(2);
		ListNode head2 = new ListNode(3);
		ListNode head3 = new ListNode(4);
		ListNode head4 = new ListNode(5);
		head.next = head1;
		head1.next = head2;
		head2.next = head3;
		head3.next = head4;
//		while(head != null){
//			System.out.println(head.val);
//			head = head.next;
//		}
		ListNode ln = new RotateList().rotateRight(head, 2);
		while(ln != null){
			System.out.println(ln.val);
			ln = ln.next;
		}
	}
	public ListNode rotateRight(ListNode head, int n) {
		if(head == null)
			return head;
		int len = 1;
		ListNode tmp = head;
		while(tmp.next != null){
			tmp = tmp.next;
			len ++;
		}
		tmp.next = head;
		n %= len;
		int step = len - n;
		while(step > 0){
			tmp = tmp.next;
			step --;
		}
		head = tmp.next;
		tmp.next = null;
		return head;
	}

}
// Definition for singly-linked list.
 class ListNode {
	int val;
	ListNode next;

	ListNode(int x) {
		val = x;
		next = null;
	}
}
时间: 2024-11-10 12:46:06

LeetCode——Rotate List的相关文章

Leetcode:Rotate List 链表旋转

Rotate List 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. 解题分析: 不同于数组旋转,数组可以随机存取,数组的旋转可以巧妙的 分成两两部分递归旋转 对于链表的旋转,实际上

[LeetCode] Rotate Image [26]

题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 原题链接(点我) 解题思路 顺时针方向旋转数组90°.这个题也是个没啥意思的题,自己画画图,找找规律.就出来了.我举一个n=4的例子还说明下规律: 通过图可以看出A[0][0] = A[3][0],.....从这些中

LeetCode: Rotate List [060]

[题目] 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. [题意] 给定一个链表L,和非负数K,要求把链表的后k个节点移到链表前 [思路] 先将指针指向指向倒数第K个节点,然后反转

LeetCode: Rotate Image [047]

[题目] You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? [题意] 给定一个nXn的二维矩阵,按时钟方向旋转90度,不能使用额外的数据结构 [思路] 从外向内逐层旋转 [代码] class Solution { public: void rotateMatrix(vec

[LeetCode] Rotate Array / List

Question: http://leetcode.com/2010/04/rotating-array-in-place.html Rotate a one-dimensional array of n elements to the right by k steps.  For instance, with n=7 and k=3, the array {a, b, c, d, e, f, g} is rotated to {e, f, g, a, b, c, d} // O (n) pub

[leetcode]Rotate Image @ Python

原题地址:https://oj.leetcode.com/problems/rotate-image/ 题意: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 解题思路1:首先沿着副对角线翻转一次,然后沿着水平中线翻转一次,就可以得到所要求的矩阵了.时间复杂度O(n^2)

2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 思路: 我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚. leetcode/discuss思路: 思路一:新建数组,复制原

[LeetCode]Rotate List

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大于或等于链表的长度n,所以实际效果等同于循环右移k%n次. 遍历单链

[LeetCode] Rotate Array

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro