代码随想录70期day3

203. Remove Linked List Elements

需要虚拟节点

python 复制代码
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        
        dummy_head = ListNode(next = head)

        # iterate the whole list to remove the node whoose value equals to the number of 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

707

Single linklists

python 复制代码
# Single Linklist
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:int) -> int:
        if index < 0 or index >= self.size:
            return -1
        
        current = self.dummy_head.next 
        for i in range(index):
            current = current.index 
        return current.index
    
    def addAtHead(self,val:int) ->None:
        self.dummy_head.next = ListNode(val,self.dummy_head.next)
        self.size += 1
    
    def addAtTail(self,val:int)->None:
        current = self.dummy_head
        while current.next:
            current = current.next 
        
        current.next = ListNode(val)
        self.size += 1
    
    def addAtIndex(self,index:int,val:int) -> None:
        
        if index < 0 or index >self.size:
            return 
        
        current = self.dummy_head
        for i in range(index):
            current = current.next 

        current.next = ListNode(val,current.next)
        self.size += 1
    
    def deleteAtIndex(self,index:int) ->None:
        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
        

Double Linklist

python 复制代码
class ListNode:
    def __init__(self,val =0,prev = None,next = None):
        self.val = val 
        self.prev = prev 
        self.next = next 

class MyLinkedList:
    def __init__(self):
        self.head = None 
        self.tail = None 
        self.size = 0

    def get(self,index:int)->int:
        if index < 0 or index >= self.size:
            return -1
        if index < self.size//2:
            current = self.head
            for i in range(index):
                current = current.next
        else:
            current = self.tail 
            for i range(self.size - index - 1):
                current = current.prev 
        
        return current.val 
    
    def addAtHead(self,val:int)->None:
        new_node =ListNode(val,None,self.head)
        if self.head:
            self.head.prev = new_node 
        else:
            self.tail = new_node 
        self.head = new_node 
        self.size +=1
    
    def addAtTail(self,val:int)->None:
        new_node = ListNode(val,self.tail,None)
        if self.tail:
            self.tail.next = new_node 
        else:
            self.head = new_node
        self.tail = new_node 
        self.size += 1
    
    def addAtIndex(self,index:int,val:int)->None:
        if index < 0 or index > self.size:
            return 
        
        if index == 0:
            self.addAtHead(val)
        elif index == self.size:
            self.addAtTail(val)
        else:
            if index <self.size //2:
                current = self.head 
                for i in range(index - 1):
                    current = current.next 
                else:
                    current = self.tail 
                    for i in range(self.size-index):
                        current = current.prev 
                new_node = ListNode(val,current,current.next)
                current.next.prev = new_node 
                current.next = new_node 
                self.size +=1 
        
    def deleteAtIndex(self,index:int)->None:
        if index < 0 or index >=self.size:
            return 
        
        if index == 0:
            self.head == self.head.next 
            if self.head:
                self.head.prev = None 
            else:
                self.tail = None 
        elif index == self.size -1:
            self.tail = self.tail.prev 
            if self.tail:
                self.tail.next = None 
            else:
                self.head = None 
        else:
            if index < self.size//2:
                current = self.head 
                for i in range(index):
                    current = current.next 
            else:
                current = self.tail 
                for i in range(self.size-index-1):
                    current = current.prev 
            current.prev.next = current.next 
            current.next.prev = current.prev 
        self.size -=1 

206

python 复制代码
# Two pointer version
class Solution:
    def reverseList(self,head:ListNode)->ListNode:
        cur = head 
        pre = None 
        while cur:
            temp  = cur.next 
            cur.next = pre 

            pre = cur 
            cur = temp 
        return pre

# Recursive version

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)
相关推荐
西部秋虫4 分钟前
YOLO 训练车牌定位模型 + OpenCV C++ 部署完整步骤
c++·python·yolo·车牌识别
CryptoPP34 分钟前
使用 KLineChart 这个轻量级的前端图表库
服务器·开发语言·前端·windows·后端·golang
18你磊哥40 分钟前
chromedriver.exe的使用和python基本处理
开发语言·python
小坏讲微服务1 小时前
Spring Cloud Alibaba 整合 Scala 教程完整使用
java·开发语言·分布式·spring cloud·sentinel·scala·后端开发
Kiri霧1 小时前
Scala 循环控制:掌握 while 和 for 循环
大数据·开发语言·scala
闲人编程1 小时前
Python的抽象基类(ABC):定义接口契约的艺术
开发语言·python·接口·抽象类·基类·abc·codecapsule
qq_172805591 小时前
Go 语言结构型设计模式深度解析
开发语言·设计模式·golang
vx_dmxq2111 小时前
【微信小程序学习交流平台】(免费领源码+演示录像)|可做计算机毕设Java、Python、PHP、小程序APP、C#、爬虫大数据、单片机、文案
java·spring boot·python·mysql·微信小程序·小程序·idea
无垠的广袤1 小时前
【工业树莓派 CM0 NANO 单板计算机】本地部署 EMQX
linux·python·嵌入式硬件·物联网·树莓派·emqx·工业物联网
lkbhua莱克瓦241 小时前
集合进阶8——Stream流
java·开发语言·笔记·github·stream流·学习方法·集合