LeetCode 61. 旋转链表

61. 旋转链表

题目:给你一个链表的头节点 head ,旋转链表,将链表每个节点向右移动 k 个位置。

链接 https://leetcode.cn/problems/rotate-list/

个人思路
  1. 这题不算难,一种思路是先循环一遍,把每个结点和位置储存在字典中,然后看要旋转几次,把新的尾结点的next重置为None,然后把原来的尾结点与头结点相连(对于旋转次数大于链表长度的情况,我们可以进行取余)
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        collection = {}
        cur = head
        i = 1
        while cur:
            collection[i] = cur
            cur = cur.next
            i += 1
        # 去除完整循环的旋转步骤
        # 只需旋转r次
        length = len(collection)
        # 对于链表长度为0或者为1或者旋转次数为0的链表可直接返回头结点
        if length == 0 or length == 1 or k== 0:
            return head
        r = k % length
        if r == 0:
            return head
        # 旋转后,最后一个结点
        node1 = collection[length - r]
        newHead = node1.next  # 新的头结点
        node1.next = None
        # 原来的尾结点与原来的头结点连接
        node2 = collection[length]
        node2.next = head
        return newHead

但因为要储存结点,内存消耗较大

  1. 可以循环两次,这样就不用消耗多余的内存来储存了
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def rotateRight(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
        cur = head
        length = 0
        while cur:
            cur = cur.next
            length += 1
        # 去除完整循环的旋转步骤
        # 只需旋转r次
        # 对于链表长度为0或者为1或者旋转次数为0的链表可直接返回头结点
        if length == 0 or length == 1:
            return head
        r = k % length
        if r == 0:
            return head
        cur = head
        i = 0
        while i < length-1:
            if i == length - r - 1:
                # 旋转后,新的头结点和尾结点
                newTail = cur
                newHead = cur.next
            cur = cur.next
            i += 1
        # 把原来的尾结点与原来的头结点相连
        cur.next = head
        # 新的尾结点的下一个结点置为空
        newTail.next = None
        return newHead
        
官方思路
  1. 闭合为环
    在这里插入图片描述
class Solution:
    def rotateRight(self, head: ListNode, k: int) -> ListNode:
        if k == 0 or not head or not head.next:
            return head
        
        n = 1
        cur = head
        while cur.next:
            cur = cur.next
            n += 1
        
        if (add := n - k % n) == n:
            return head
        
        cur.next = head
        while add:
            cur = cur.next
            add -= 1
        
        ret = cur.next
        cur.next = None
        return ret

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/rotate-list/solution/xuan-zhuan-lian-biao-by-leetcode-solutio-woq1/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值