!!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. 掌控安全学院SQL注入靶场

    注入的本质:用户输入的字符被带入数据库中并且执行了 靶场地址:http://inject2.lab.aqlab.cn/ 第六关:http://inject2.lab.aqlab.cn/Pass-06/ ...

  2. [部署日记]Android Studio在安装完后,sdk依旧提示SDK emulator directory is missing

    问题起源: 今天在闲着没事搞了个go的rest-api,用postman测试了一下可行,于是一拍大腿决定写一个安卓手机程序,于是一拍大腿重新下载了卸载没多久因为没空间放原神的Android Studi ...

  3. SAP 附件功能 PRD环境无法删除 VIEW_ATTA

    如图:界面上面没有打勾确认按钮 解决方案:来源网址 How to disable, delete and edit buttons function in attachment list. | SAP ...

  4. 从main_phase跳回reset_phase的方式

    在main_phase中调用: phase.jump(uvm_reset_phase::get()); 注意需要防止进入死循环.

  5. linux dma

    https://bootlin.com/pub/conferences/2015/elc/ripard-dmaengine/ripard-dmaengine.pdf https://biscuitos ...

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

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

  7. VMware vSphere Client(4.1/5.0/5.1/5.5/6.0) 客户端下载地址

    前言 VMware作为商业虚拟化方案的佼佼者不知不觉中已经成长为一颗苍天大树,面对OpenStack和Docker的夹击希望VMware可以继续勇往直前,从vSphere 6.5开始终于彻底告别Cli ...

  8. 蓝桥杯训练赛二-问题 B

    字符串的输入输出处理. 输入 第一行是一个正整数N,最大为100.之后是多行字符串(行数大于N), 每一行字符串可能含有空格,字符数不超过1000. 输出 先将输入中的前N行字符串(可能含有空格)原样 ...

  9. Django Rest Frame work 如何使用serializers序列化

    Django Rest Frame work 如何使用serializers序列化       Django Rest Framework提供了serializers模块,用于序列化和反序列化模型实例 ...

  10. abap screen页签开发注意事项

    问题描述:我比较懒,开发程序的时候所有的页签都是直接公用主程序的按钮功能,这就导致,当我按了按钮之后,SY-UCOMM里保存的是我自建按钮的code, 如果不进行页签跳转,或者按其他按钮的话,直接按下 ...