代码随想录训练营第三天: 203.移除链表元素 707.设计链表 206.反转链表

本文介绍了如何在Python中使用类定义双向链表结构,包括添加元素(头部、尾部和指定位置)、获取元素、删除指定位置的元素以及反转链表的基本操作。
摘要由CSDN通过智能技术生成
class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        dummy_head = ListNode(next = head)
        
        # 遍历列表并删除值为val的节点
        current = dummy_head
        while current.next:
            if current.next.val == val:
                current.next = current.next.next
            else:
                current = current.next
        
        return dummy_head.next

第一题很简单,虚拟头结点

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

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


    def get(self, index):
        """
        :type index: int
        :rtype: int
        """
        cur=self.dummy_head
        if index>=0 and index<self.size:
            while index>=0:
                cur=cur.next
                index-=1
            return cur.val
        else:
            return -1

        


    def addAtHead(self, val):
        """
        :type val: int
        :rtype: None
        """
        self.dummy_head.next=ListNode(val,self.dummy_head.next)
        self.size+=1
        


    def addAtTail(self, val):
        """
        :type val: int
        :rtype: None
        """
        cur=self.dummy_head
        while cur.next:
            cur=cur.next
        cur.next=ListNode(val,None)
        self.size+=1


    def addAtIndex(self, index, val):
        """
        :type index: int
        :type val: int
        :rtype: None
        """
        cur=self.dummy_head
        if index>=0 and index<=self.size:
            while index>0:
                cur=cur.next
                index-=1
            cur.next=ListNode(val,cur.next)
            self.size+=1
        else:
            return


    def deleteAtIndex(self, index):
        """
        :type index: int
        :rtype: None
        """
        cur=self.dummy_head
        if index>=0 and index<self.size:
            while index>0:
                cur=cur.next
                index-=1
            cur.next=cur.next.next
            self.size-=1
        else:
            return



第二题,只要初始化成功,逻辑也不

class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        pre=ListNode(next=head)
        cur=head
        if head==None:
            return None
        while cur:
            cur1=cur.next
            cur.next=pre
            pre=cur
            cur=cur1
        head.next=None
        return pre

翻转链表这题想了很久,看了动画终于理清思路,但也不知道总结啥,唉,这题以前做过,时间长了忘了又不会了

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值