LeetCode OJ 61. Rotate List 考虑边界条件

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

61. Rotate List

My Submissions

QuestionEditorial Solution

Total Accepted: 71917 Total Submissions: 311425 Difficulty: Medium

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.

Subscribe to see which companies asked this question

Show Tags

Show Similar Problems

Have you met this question in a real interview? 

Yes

No

Discuss Pick One

题目不难,但是边界条件十分让人抓狂。重点考虑以下三个边界case:

  1. 右移的k值比链表的本身长度还要长。这个时候需要对长度取模。
  2. k=0时,直接返回head即可
  3. head为null时,返回null

个人在使用单链表的时候,十分喜欢起哨兵作用的头结点。以下是我的AC代码:

public class RotateList {
	public static void main(String[] args) {
		ListNode h = new ListNode(1);
		h.next = null;
		rotateRight(h, 1);
	}

	public static ListNode rotateRight(ListNode head, int k) {
		if (head == null) return null;

		ListNode h = new ListNode(0);
		h.next = head;
		ListNode p1 = h, p2 = h, p = h;

		int len = 0;
		while (p.next != null) {
			p = p.next;
			len++;
		}
		k %= len;
		if (k == 0) return head;

		for (int i = 0; i < k; i++) p2 = p2.next;
		while (p2.next != null) {
			p1 = p1.next;
			p2 = p2.next;
		}

		h.next = p1.next;
		p2.next = head;
		p1.next = null;
		return h.next;
	}
}
时间: 2024-10-08 06:36:08

LeetCode OJ 61. Rotate List 考虑边界条件的相关文章

LeetCode OJ 61. 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. Subscribe to see which companies asked this question 解答: 不是很懂这道题的通过率

&lt;LeetCode OJ&gt; 48. Rotate Image

Total Accepted: 69879 Total Submissions: 199786 Difficulty: Medium 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? Subscribe to see which companies asked this

【一天一道LeetCode】#61. Rotate List

一天一道LeetCode系列 (一)题目 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. (二)解题 本题的思路: 1.找到倒数第K个节点 2.将k以后的节点移动到前面来,与头结点

LeetCode OJ: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]. 将数组的内容倒置,看例子就知道是什么意思: 1 class Solution { 2 public: 3 void rotate(vector<int>& nums, int k) { 4 if(

笔试题74. LeetCode OJ (61)

    Rotate List    这个题的意思旋转链表,更具体点的意思右移链表,移出去的节点放到头部前面,结合着题目给出的例子还是很好理解的. 这个题的主要思路是:摘取从末尾到头的k个节点,然后将他们放到头部. 需要注意的是,上面说的k并不一定等于传入的k的值,因为这个k很可能比链表的长度还大.所以我主要思路是:遍历一遍链表,找到链表的长度n,然后k%=n(这时候k<n,我们更喜欢的是此时k=0),这样就可以找出实际需要移动的节点的个数,然后将链表的最后k个节点放到链表的前面就行了.大概方法

LeetCode OJ 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 pro

【Leetcode】61. Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative. Example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. Tips:右移结点,过程如下: k=2,右移两次: ①5->1->2->3->4 ②4->5->1-&g

LeetCode OJ Linked List: 24题、148题和61题

题外话:最近打算做一些LeetCode OJ上面的练习题了,当然很多前辈都已经写过若干解题报告了.坦白说,也正是因为前辈们的贡献,才让我们学习到了很多知识.所以,我一直都在犹豫到底要不要写解题报告多此一举呢?当然,我水平很渣啊.我觉得虽然,有很多很好的解题报告可参考了,但是自己对待这件事的态度又是另外一回事,记录下来,完全是提醒自己这段时间是有计划的,是要做题的. 24题:Swap Nodes in Pairs 题目分析:链表长度为奇数时,最后一个节点不用再调整啦.当然了,不能改变节点的值,否则

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个节点放到前面,可以采用快慢指针处理.不