Leetcode日练笔记35 [Linked List专题] #138 #61 Copy List with Random Pointer & Rotate List

本文主要探讨LeetCode中的两个链表问题:138题-复制带随机指针的链表,通过创建新的节点并维护节点间的连接实现深拷贝;61题-旋转链表,通过移动指针实现链表的右旋。文章介绍了解题思路和Python实现方法,并分享了运行时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#138 Copy List with Random Pointer

A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null.

Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.

For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.

Return the head of the copied linked list.

The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:

  • val: an integer representing Node.val
  • random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.

Your code will only be given the head of the original linked list.

Example 1:

 解题思路:

Deep Copy是为了避免改副本时,原来的数据也被改变。所以就创建了另外一个地址的副本。相反的是shallow copy。副本和原本都是指向同一个对象。副本改了原本也被改变。

Python有直接的funciton可以用。

import copy之后,copy.deepcopy(head)就行。

"""
# Definition for a Node.
class Node:
    def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
        self.val = int(x)
        self.next = next
        self.random = random
"""
import copy

class Solution:
    def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
        # deep copy do not share the same reference as the original one
        deepcopy = copy.deepcopy(head)
        return deepcopy

runtime:

参考一下19ms的:

 

新创建一个dict,存原本和副本有个对应的关系。 先把点存完,然后再把next 和random指针依照原本,指向副本里的点。

#61 Rotate List

Given the head of a linked list, rotate the list to the right by k places.

Example 1:

Input: head = [1,2,3,4,5], k = 2
Output: [4,5,1,2,3]

解题思路:

用三个pointer:h, tail, sever 分别标注头尾和切割点。head = sever.next, sever.next = None, tail.next = h.来完成rotate。

一开始三个指针都指向head,然后while循环往后送。切割点是算出来链的长度后,用这个长度减去k除以长度后得到的余数再减一得到的。

# 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]:
        if not head or k == 0: return head
        
        length, sever, h, tail = 1, head, head, head

        # send tail pointer to the end node
        while tail.next:
            tail = tail.next
            length += 1
        
        if length == 1 or k%length == 0: return head
        
        k = length - k % length - 1
        
        # get to the sever point
        while k:
            sever = sever.next
            k -= 1
            
        head = sever.next
        sever.next = None
        tail.next = h
        
        return head

runtime: 

 参考forum看到,length == 1可以用not head.next来替换。其他部分的思路都相似。链表专题16题做完了。接下来二叉树专题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值