!!Given the head of a linked list, rotate the list to the right by k places.!!
 
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def rotateRight(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        if head == None:
            return []
        count = 1
        temp = head
        while (temp.next != None):
            count +=1
            temp = temp.next
            # count = length, so the length remaining same seq is count - k -1
 
        if k == 0:return head;
        #else
        head = temp.next
        temp.next = None
        while(count -k -1): #ensure the amount of reversed nodes
            #to reverse
            head = temp.next
            temp.next = None #clearance
            return head
 
A better way:
#先把链表首尾相连,再找到位置断开循环
class Solution(object):
def rotateRight(self, head, k):
if head is None or head.next is None: return head
start, end, len = head, None, 0
while head:
end = head
head = head.next
len += 1
end.next = start
pos = len - k % len
while pos > 1:
start = start.next
pos -= 1
ret = start.next
start.next = None
return ret

Leetcode61的更多相关文章

  1. [LeetCode61]Rotate List

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...

  2. [Swift]LeetCode61. 旋转链表 | Rotate List

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  3. Leetcode61.旋转链表

    链表中的点已经相连,一次旋转操作意味着: 先将链表闭合成环 找到相应的位置断开这个环,确定新的链表头和链表尾 class Solution{ public: ListNode* rotateRight ...

  4. Leetcode61. Rotate List旋转链表

    给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...

  5. LeetCode链表解题模板

    一.通用方法以及题目分类 0.遍历链表 方法代码如下,head可以为空: ListNode* p = head; while(p!=NULL) p = p->next; 可以在这个代码上进行修改 ...

随机推荐

  1. 树莓派zero W ,连接墨水屏(2.13inch_e-Paper_HAT (V3))

    墨水瓶型号:    2.13inch_e-Paper_HAT (V3) https://www.waveshare.net/wiki/2.13inch_e-Paper_HAT 与树莓派 Zero 连接 ...

  2. Linux的top命令原理简单了解

    top命令描述机器的cpu.内存等状态信息. 每3s刷新一次. 是procps工具集中的一个,该工具集还包括free.ps等等 top命令的代码实现逻辑是:由内核动态生成一个伪文件系统,提供一个内核状 ...

  3. 给自己提个醒,渲染模版引擎handlebars已经足够好用了,不要再到处乱看浪费时间了。

    <html><body onload="renderHello()"><div id="target">Loading... ...

  4. 3-MIRO发票校验设置默认税码-OMR2

  5. qt vnc 远程左桌面

    设备端启动服务器 ./testqt -qws -display VNC:0 客服端启动监控端 VNC Viewer 4

  6. 取消Andorid设备的严格模式

    在做文件拍照选择等应用时,发现7.0以上手机需要主动关闭严格模式才能让代码不报错 通过关闭严格模式绕过fileprovider 权限的控制的方法,绕过FileProvider在应用间共享文件的限制 7 ...

  7. NC16644【字符串的展开】

    正确代码: #include <iostream>#include <algorithm>using namespace std;bool IsSame(char a, cha ...

  8. 百度云+Zotero进行知识管理的方法

    首先,要在zotero的首选项的文件与文件夹里去自定义你的zotero文件夹,这个文件夹就是你的zotero软件的文档存储编辑的文件夹,本来默认是在电脑用户里自动创建的,比如hp/user/zoter ...

  9. Advent of Code 2015: Day 10

    JP's Blog GITHUB * FLICKR * RESUME  Search Programming Reviews Photography Maker Writing Research RS ...

  10. OJ19

    1 // we have defined the necessary header files here for this problem. 2 // If additional header fil ...