leetcode 61Rotate List
来源:互联网 发布:ubuntu配置lamp环境 编辑:程序博客网 时间:2024/04/24 04:08
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if not head or not head.next or k==0:
return head
n=self.getSize(head)
k%=n
dummy=ListNode(0)
dummy.next=head
p=head
for i in range(k):
p=p.next
for j in range(n-k-1):
p=p.next
head=head.next
p.next=dummy.next
dummy.next=head.next
head.next=None
return dummy.next
def getSize(self,head):
head=head.next
n+=1
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def rotateRight(self, head, k):
"""
:type head: ListNode
:type k: int
:rtype: ListNode
"""
if not head or not head.next or k==0:
return head
n=self.getSize(head)
k%=n
dummy=ListNode(0)
dummy.next=head
p=head
for i in range(k):
p=p.next
for j in range(n-k-1):
p=p.next
head=head.next
p.next=dummy.next
dummy.next=head.next
head.next=None
return dummy.next
def getSize(self,head):
n=0
while head:head=head.next
n+=1
return n
https://www.youtube.com/watch?v=7OBo9fBp1FE
0 0
- LeetCode 61: Rotate List
- [leetcode 61] Rotate List
- [leetcode #61]Rotate List
- leetcode || 61、Rotate List
- leetcode[61]:Rotate List
- LeetCode 61: Rotate List
- LeetCode(61) Rotate List
- LeetCode 61 Rotate List
- leetcode 61: Rotate List
- Leetcode#61||Rotate List
- [Leetcode]#61 Rotate List
- leetcode 61:Rotate List
- 【leetcode】【61】Rotate List
- 【LeetCode-61】Rotate List
- LeetCode 61 - Rotate List
- ( Leetcode 61 ) Rotate List
- leetcode-Rotate List-61
- LeetCode 61 Rotate List
- leetcode62Unique Paths
- Problem B: 点在圆内吗?
- Caffe学习系列(2):数据层及参数
- [Python]模块和包
- 浅谈HTTP
- leetcode 61Rotate List
- Problem C: 重载字符的加减法
- leetcode60Permutation Sequence
- 通信协议——Http、TCP、UDP
- 区块链共识机制(zhai)
- eclipse maven项目部署到tomcat中 启动后classes文件夹下没有文件
- Problem D: 字符构成的图形
- ROS之nodelet的使用
- HTTP的长连接和短连接