代码随想录算法训练营Day3 | 链表理论基础,203.移除链表元素,707.设计链表,206.反转链表

本文探讨链表基础,包括与数组的区别,并详细解析LeetCode上的203.移除链表元素、707.设计链表和206.反转链表问题。强调虚拟头结点在链表操作中的重要性,同时提供解题技巧和代码实现,适合深化链表理解。
摘要由CSDN通过智能技术生成
详细布置 

链表理论基础 ✅

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

文章链接:代码随想录

203.移除链表元素 ✅

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

题目链接/文章讲解/视频讲解::代码随想录

# 203
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        # 创建虚拟头部节点
        dummy_head = ListNode(next = head)
        
        current = dummy_head
        while current.next: # 记得判断curr.next是否为空!
            if current.next.val == val:
                current.next = current.next.next
            else:
                current = current.next
        
        return dummy_head.next

707.设计链表 AC

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

题目链接/文章讲解/视频讲解:代码随想录

# 707
# (版本一)单链表法
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 get(self, index):
        if index < 0 or index >= self.size:
            return -1
        current = self.dummy_head.next
        while index:
            current = current.next
            index -= 1
        return current.val

    def addAtHead(self, val):
        self.dummy_head.next = ListNode(val, self.dummy_head.next)
        self.size += 1

    def addAtTail(self, val):
        current = self.dummy_head
        while current.next:
            current = current.next
        current.next = ListNode(val)
        self.size += 1

    def addAtIndex(self, index, val):
        if index < 0 or index > self.size:
            return
        current = self.dummy_head
        while index:
            current = current.next
            index -= 1
        current.next = ListNode(val, current.next) # 两行并一行写
        self.size += 1

    def deleteAtIndex(self, 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.反转链表 ✅

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

题目链接/文章讲解/视频讲解:代码随想录

# 206
# 双指针法
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        cur = head
        pre = None
        while cur:
            temp = cur.next
            cur.next = pre
            pre = cur
            cur = temp
        return pre
# 206
# 递归法
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        return self.reverse(head, None)

    def reverse(self, cur, pre):
        if cur == None: return pre
        temp = cur.next
        cur.next = pre
        return self.reverse(temp, cur)

这里注意一下OOP的规范!

  • self.reverse(head, None):在reverseList方法中调用reverse时,使用self.reverse而不是单独的reverse。这告诉Python去查找当前对象的reverse方法,而不是在外部环境中寻找一个名为reverse的函数。
  • def reverse(self, cur, pre)::在定义reverse方法时,添加self作为第一个参数,以便在该方法内部可以使用这个类实例的其他方法或属性。这种方式是面向对象编程的一部分,利用了Python的类特性。

总结

递归的博客:递归解题三部曲(每一级的功能都是一样的,因此只需关注一级递归的解决过程

        即:

  • 找整个递归的终止条件:递归应该在什么时候结束?
  • 本级递归应该做什么:在这一级递归中,应该完成什么任务?
  • 找返回值:应该给上一级返回什么信息?

        补充例题目:

        到这里是三个例题,待看三道题套路解决递归问题 | lyl's blog

  

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值