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个节点,然后记录倒数第K+1个节点,然后再指针变化就可以了

但是出错!原因是如果K正好是head,这时候K之前的那个节点没法处理

下面附上代码:

public ListNode rotateRight(ListNode head, int k) {
        if (head == null) {
			return null;
		}
       ListNode temp = head;
		int len = 0;
		while(temp!=null){
			temp = temp.next;
			len++;
		}
		k = k%len;

		if (head.next == null || k == 0) {
			return head;
		}
		ListNode pointer = FindKToTail(head, k);

		if (pointer != null && pointer.next != null) {
			ListNode pointer1 = pointer.next;
			ListNode result = pointer1;
			pointer.next = null;
			while (pointer1.next != null) {
				pointer1 = pointer1.next;
			}
			pointer1.next = head;
			return result;
		} else {
			return null;
		}

	}

	public ListNode FindKToTail(ListNode head, int k) {
		if (head == null || k == 0) {
			return null;
		}
		ListNode fast = head;
		for (int i = 0; i < k; i++) {
			if (fast.next != null) {
				fast = fast.next;
			} else {
				return head;
			}
		}
		ListNode slow = head;

		while (fast.next != null) {
			fast = fast.next;

			slow = slow.next;
		}
		return slow;
	}
时间: 2024-10-01 07:08:02

leetcode之Rotate List的相关文章

LeetCode --- 61. 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. 这道题的要求是向右旋转链表k步. 其实就是把链表后面l-k个节点放到前面,可以采用快慢指针处理.不

【Leetcode】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? 思路:第一种思路是一层一层的进行旋转,比较直观:第二种思路则比较取巧,首先沿着副对角线翻转一次,然后沿着水平中线翻转一次. 代码一: class Solution { public: void rotate(vector<

[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个元素移动到链表的头部. 这道题的本质就是寻找链表的

【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. start with an example. Given [0,1,2], rotate 1 steps to the right -&

LeetCode:Rotate Array

1.题目名称 Rotate Array(平移数组) 2.题目地址 https://leetcode.com/problems/rotate-array/ 3.题目内容 英文:Rotate an array of n elements to the right by k steps. 中文:将一个长度为n的数组,向右平移k个单位 4.解题方法1 一个比较易于理解的方法是新开辟一个与原数组等长的数组,循环考察原数组的元素,将它们放到新数组中平移后的位置上,最后再将新数组上的元素赋回原数组. 一段可以

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

[LeetCode]189.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

leetcode 61 Rotate List ----- java

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个节点,然后以这个节点为开头,重新组成一个链表. 需要注意的就是如果k大于链表长度len

LeetCode[Array]: 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? 参考LeetCode[Array]: Spiral Matrix II的迭代思路,先完成最外环的旋转,然后依次旋转内环.我的C++代码如下: void rotate(vector<vector<int> >

[Leetcode][048] Rotate Image 略详细 (Java)

题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有较高要求. 放了两个版本的答案,第一个版本是自己写的,第二个是目前最佳答案的Java改写. [代码注释] Solution1: 思路比较直接.既然要求in-place,那就在修改之前,先保存原先在那个位置上的值,然后尾巴咬尾巴的去修改:大意可以参考下图 Solution2: 偏数学的方法,先把矩阵“