LeetCode-61-Rotate List 链表水题
2017-09-20 16:32
357 查看
# 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 head==None:return []
cnt=1
last=head
while(last.next!=None):
last=last.next
cnt+=1
k%=cnt
if k==0:return head
curHead=head
last.next=head
print last.val
while(cnt>k+1):
cnt-=1
curHead=curHead.next
ans=curHead.next
curHead.next=None
return ans
# 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 head==None:return []
cnt=1
last=head
while(last.next!=None):
last=last.next
cnt+=1
k%=cnt
if k==0:return head
curHead=head
last.next=head
print last.val
while(cnt>k+1):
cnt-=1
curHead=curHead.next
ans=curHead.next
curHead.next=None
return ans
相关文章推荐
- leetcode 61 --Rotate List 链表 循环/断开 翻转
- 链表系列-把链表向右旋转k个位置LeetCode#61. Rotate List
- leetcode解题61. Rotate List java版(旋转链表)
- [leetcode] 【链表】 61. Rotate List
- leetCode 61.Rotate List (旋转链表) 解题思路和方法
- leetcode 61. Rotate List 向右旋转k个元素 + 链表环遍历
- 【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解题-链表(2.2.6)RotateList
- LeetCode 61. Rotate List