leetcode-61-旋转链表

题目描述:

方法一;

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if not head:
            return None
        if not head.next:
            return head
        old_tail = head
        n = 1
        while old_tail.next:
            old_tail = old_tail.next
            n += 1
        old_tail.next = head
        new_tail = head
        for i in range(n-1-k%n):
            new_tail = new_tail.next
        new_head = new_tail.next
        new_tail.next = None
        return new_head

 

posted @ 2019-07-12 17:34  oldby  阅读(127)  评论(0编辑  收藏  举报