代码随想录Day3:链表理论+移除链表元素+设计链表+反转链表

一、链表理论基础

建议:了解一下链接基础,以及链表和数组的区别

文章链接:https://programmercarl.com/%E9%93%BE%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html

链表的定义

class ListNode:
    def __init__(self, val, next=None):
        self.val = val
        self.next = next

二、移除链表元素(203.)

建议: 本题最关键是要理解 虚拟头结点的使用技巧,这个对链表题目很重要。

题目链接/文章讲解/视频讲解::https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html

时间复杂度:O( n n n )
空间复杂度:O(1)

class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        dummy_head = ListNode(next = head) # 创建虚拟头节点
        current = dummy_head # 用来遍历链表的指针
        while current.next:
            if current.next.val == val: # 删除值为val的节点
                current.next = current.next.next
            else:
                current = current.next
        return dummy_head.next

三、设计链表(707.)

建议: 这是一道考察 链表综合操作的题目,不算容易,可以练一练 使用虚拟头结点

题目链接/文章讲解/视频讲解:https://programmercarl.com/0707.%E8%AE%BE%E8%AE%A1%E9%93%BE%E8%A1%A8.html

# 节点的定义
class ListNode:
    def __init__(self, val = 0, next = None):
        self.val = val
        self.next = next

# 单链表的定义
class MyLinkedList:

    def __init__(self): # 构造函数:用于初始化链表
        self.dummy_head = ListNode()
        self.size = 0

    def printLinkedList(self): # 打印链表
        current = self.dummy_head.next
        print('链表为:')
        while current:
            print(current.val)
            current = current.next

    def get(self, index: int) -> int: # 用于获取链表中下标为index的节点的值
        if index < 0 or index >= self.size:
            return -1
        current = self.dummy_head.next
        for i in range(index):
            current = current.next
        return current.val
    def addAtHead(self, val: int) -> None:  # 用于在表头插入新节点
        new = ListNode(val, self.dummy_head.next)
        self.dummy_head.next = new
        self.size += 1

    def addAtTail(self, val: int) -> None: # 用于在表尾插入新节点
        new = ListNode(val)
        current = self.dummy_head
        for i in range(self.size):
            current = current.next
        current.next = new
        self.size += 1

    def addAtIndex(self, index: int, val: int) -> None: # 用于在某节点前插入新节点
        if index < 0 or index > self.size: # 要求index等于链表的长度时新节点被追加到链表的末尾,则index可以等于size
            return
        current = self.dummy_head
        for i in range(index):
            current = current.next
        new = ListNode(val, current.next) # 记得要在该节点后面接上剩余的链表
        current.next = new
        self.size += 1

    def deleteAtIndex(self, index: int) -> None: # 用于删除下标为index的节点
        if index < 0 or index >= self.size:
            return
        current = self.dummy_head
        for i in range(index):
            current = current.next
        current.next = current.next.next
        self.size -= 1

四、反转链表(206.)

建议先看我的视频讲解,视频讲解中对 反转链表需要注意的点讲的很清晰了,看完之后大家的疑惑基本都解决了。

题目链接/文章讲解/视频讲解:https://programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html

方法1:双指针法

定义一个pre指针,初始化为null,定义一个cur指针,指向头结点。
在每个循环中反转节点的next指针,并将pre和cur指针向后移动。
直到cur 指针为null时,循环结束。
在这里插入图片描述

时间复杂度:O( n n n)
空间复杂度:O(1)

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        cur = head   
        pre = None
        while cur:
            temp = cur.next # 保存一下 cur的下一个节点,因为接下来要改变cur->next
            cur.next = pre #反转
            #更新pre、cur指针
            pre = cur
            cur = temp
        return pre

方法2:递归法

和双指针法的逻辑一样,只是把循环改为递归的形式,代码更简洁了

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        return self.reverse(head, None)
    def reverse(self, cur: ListNode, pre: ListNode) -> ListNode:
        if cur == None:
            return pre
        temp = cur.next
        cur.next = pre
        return self.reverse(temp, cur)
  • 13
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值