关闭

【Leetcode】Rotate List

276人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/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的边界情况

算法:

	public ListNode rotateRight(ListNode head, int k) {
		ListNode p = head, rear = head;
		int length = 0;
		while (p != null) {// 计算表长
			length++;
			if (p.next == null) {
				rear = p;// rear指向表尾
			}
			p = p.next;
		}
		if (head == null || length == 0) {
			return head;
		}
		if (k > length) {
			k = k % length;
		}
		if (k == 0 || k == length) {
			return head;
		}

		p = head;
		int rotateIndex = length - k;
		while (--rotateIndex > 0) {
			p = p.next;
		} // 此时p指向倒数k+1一个位置

		rear.next = head;
		head = p.next;
		p.next = null;
		return head;
	}


0
0
查看评论

(Java)LeetCode-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-&...
  • u012848330
  • u012848330
  • 2016-10-06 17:52
  • 264

【LEETCODE】61- Rotate List [Python]

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,...
  • aliceyangxi1987
  • aliceyangxi1987
  • 2016-02-24 12:24
  • 736

LeetCode 61 — Rotate List(C++ Java Python)

题目:http://oj.leetcode.com/problems/rotate-list/ Given a list, rotate the list to the right by k places, where k is non-negative....
  • dragon_dream
  • dragon_dream
  • 2014-02-24 22:11
  • 2550

[leetcode-48]Rotate Image(java)

问题描述: 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?分析:题意非...
  • zdavb
  • zdavb
  • 2015-08-02 21:59
  • 803

(Java)LeetCode-48. 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 ...
  • u012848330
  • u012848330
  • 2016-09-29 15:36
  • 258

[LeetCode][Java] Rotate Image

题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Cou...
  • Evan123mg
  • Evan123mg
  • 2015-07-14 19:57
  • 597

leetcode刷题系列C++-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...
  • u013676711
  • u013676711
  • 2016-02-22 11:18
  • 253

leetcode Rotate list (链表旋转)的python实现

题目如下: 题目解释: 给定一个链表,将链表末尾的k个结点移动到最前面。 思路: 采用 fast-slow 指针的方法,令fast指针先移动k步,步长为1。然后两个指针同时移动,当fast指针到达最末尾时,将fast指向head,slow指向None,则完成旋转。 注:题目中的k有可能大于链表...
  • u012560212
  • u012560212
  • 2017-05-12 18:35
  • 357

<leetcode>396 Rotate Function

396 Rotate Function Given an array of integers A and let n to be its length. Assume Bk to be an array obtained by rotating the array A k positions cl...
  • Vipin_Pei
  • Vipin_Pei
  • 2016-09-14 16:57
  • 612

Leetcode:189. Rotate Array(JAVA)

【问题描述】 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the a...
  • u010183658
  • u010183658
  • 2016-03-21 16:31
  • 318
    个人资料
    • 访问:567540次
    • 积分:8658
    • 等级:
    • 排名:第2678名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论