【LeetCode】61. Rotate List 解题报告(Python)

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/rotate-list/description/

题目描述:

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->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL

Example 2:

Input: 0->1->2->NULL, k = 4
Output: 2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: 0->1->2->NULL
rotate 4 steps to the right: 2->0->1->NULL

题目大意

给出了链表和数字k。重复k次下面的操作:把链表最后的一个节点移动到开头。返回新的链表。

解题方法

首先,可以从Example 2也能看出来,存在k>len的情况,这样必须求余运算,否则肯定超时。即要求链表的长度。

其次,如果求余之后,知道了移动几次,本质上就是把链表的后面k个节点移动到开头去。注意是平移,顺序不变的。所以要找到后面的k个节点,那么需要用到19. Remove Nth Node From End of List类似的方法,用两个距离为k的指针进行平移操作,当前面的到达了末尾,那么后面的正好是倒数第k个。

找到倒数第k个之后,那么把这个节点和之前的节点断开,把后面的这段移到前面去即可。

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if not head or not head.next: return head
_len = 0
root = head
while head:
_len += 1
head = head.next
k %= _len
if k == 0: return root
fast, slow = root, root
while k - 1:
fast = fast.next
k -= 1
pre = slow
while fast.next:
fast = fast.next
pre = slow
slow = slow.next
pre.next = None
fast.next = root
return slow

日期

2018 年 6 月 23 日 ———— 美好的周末要从刷题开始

【LeetCode】61. Rotate List 解题报告(Python)的更多相关文章

  1. [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. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  3. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  4. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  5. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  8. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. Python基础之列表内置方法

    目录 1. 列表 1.1 序列 1.2 通用的序列操作 1.3 列表的基本操作 1.4 列表方法 1. 列表 数据结构:以某种方式(如通过编号)组合起来的元素(如数,字符乃至其他数据结构)集合. 在p ...

  2. 基于 芯片 nordic 52832 rtt 调试(Mac 电脑)

    代码配置 // <e> NRF_LOG_BACKEND_SERIAL_USES_UART - If enabled data is printed over UART //======== ...

  3. 17.Power of Four-Leetcode

    #define IMIN numeric_limits<int>::min() #define IMAX numeric_limits<int>::max() class So ...

  4. 点击下拉选择触发事件【c#】

    <asp:DropDownList ID="ddlRegionList" runat="server" AutoPostBack="true&q ...

  5. 学习Java的第四天

    一.今日收获 1.java完全手册的第一章 2.   1.6节了解了怎么样用记事本开发java程序 与用Eclipse开发 2.完成了对应例题 二.今日难题 1.一些用法容易与c++的混淆 2.语句还 ...

  6. 零基础学习java------day8------javabean编写规范,继承,static关键字,代码块,单例设计模式

    0. 今日内容提要 1. javabean书写规范 javabean:一个普通的类,用来描述事物的类,里面不包含任何的业务逻辑,只是用来存储数据. 比如:Teacher,Student,Mobile. ...

  7. Gradle—Android配置详解

    参考[1]彻底弄明白Gradle相关配置       [2]Android Studio gradle配置详解

  8. Swift alert 倒计时

    let title: String = "您的开奖时间为" let time: String = "2017-10-23 12:23:18" let count ...

  9. 【Linux】【Basis】Grub

    GRUB(Boot Loader):   1. grub: GRand Unified Bootloader grub 0.x: grub legacy grub 1.x: grub2   2. gr ...

  10. fatal: unable to access 'https://github.com/xxxxx/xxxx.git/': Failed to connect to github.com port 443: Timed out

    今天使用git push的时候提示"fatal: unable to access 'https://github.com/xxxxx/xxxx.git/': Failed to conne ...