代码随想录算法训练营第3天| 203.移除链表元素 、707.设计链表、 206.反转链表

  • 今日学习的文章链接,或者视频链接

第二章 链表part01

  • 自己看到题目的第一想法

203:

删除元素需要保留前面的指针

边界条件的处理:当head.val是需要删除的元素时

class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        while head and head.val == val:
            head = head.next

        if not head:
            return None

        temp_prev = head
        temp_cur = head.next

        while temp_cur:
            if temp_cur.val == val:
                temp_prev.next = temp_cur.next
            else:
                temp_prev = temp_prev.next
            temp_cur = temp_cur.next
        
        return head

707:

受203和cs61b的启发,使用dummyhead后链表的插入和删除操作可以简化很多

206:

保留prev指针

  • 看完代码随想录之后的想法

203:

虚拟头节点:dummyhead

不用分两种情况了,直接开删:

        dummyhead = ListNode()
        dummyhead.next = head
        cur = dummyhead

        while cur.next:
            if cur.next.val == val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return dummyhead.next

707:

注意addatindex时的边界条件与其他的函数不一样(index == self.size时是有效的)

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

class MyLinkedList:

    def __init__(self):
        self.size = 0
        self.dummy_head = Node()

    def get(self, index: int) -> int:
        if index < 0 or index >= self.size:
            return -1
        current = self.dummy_head
        for _ in range(index + 1):
            current = current.next
        return current.val

    def addAtHead(self, val: int) -> None:
        new_node = Node(val)
        new_node.next = self.dummy_head.next
        self.dummy_head.next = new_node
        self.size += 1


    def addAtTail(self, val: int) -> None:
        new_node = Node(val)
        cur = self.dummy_head
        while (cur.next):
            cur = cur.next
        cur.next = new_node
        self.size += 1


    def addAtIndex(self, index: int, val: int) -> None:
        if index < 0 or index > self.size:
            return

        current = self.dummy_head
        for _ in range(index):
            current = current.next
        new_node = Node(val)
        new_node.next = current.next
        current.next = new_node
        self.size += 1


    def deleteAtIndex(self, index: int) -> None:
        if index < 0 or index >= self.size:
            return -1
        current = self.dummy_head
        for _ in range(index):
            current = current.next
        current.next = current.next.next
        self.size -= 1

206:

非递归:

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        cur = head
        prev = None
        while cur:
            temp = cur.next
            cur.next = prev

            prev = cur
            cur = temp
        return prev

递归:

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        def reverse(pre,cur):
            if not cur:
                return pre
                
            tmp = cur.next
            cur.next = pre

            return reverse(cur,tmp)
        return reverse(None,head)
  • 自己实现过程中遇到哪些困难

203:

我定义了prev和cur,思路是要删cur,需要知道prev,对比发现不用这么麻烦,直接用cur.next = cur.next.next删除更好

707:

忘了size++,size--, 边界条件判断

206:

递归一头雾水,听了讲解才明白,还是双指针解法更易懂

  • 今日收获,记录一下自己的学习时长

203:

对链表元素的删除:cur.next = cur.next.next

707:

链表的基本操作

206:

双指针

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值