【代码随想录】链表

2024.5.11-2024.5.15

移除链表元素

python 复制代码
		#判断头节点是否空,
		#并且头节点为要删除的值,头节点向后移
        while head and head.val==val:
            head=head.next
        if not head: return
        cur=head
        #当前指针指向的下一个元素为val,当前指针指向下一个的下一个
        #否则,当前指针向后移动一位
        while cur.next:
            if cur.next.val==val:
                cur.next=cur.next.next
            else:
                cur=cur.next
        return head

设计链表

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

class MyLinkedList(object):

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


    def get(self, index):
        """
        :type index: int
        :rtype: int
        """
        if index<0 or index>=self.size:
            return -1
        cur=self.head
        for i in range(index+1):
            cur=cur.next
        return cur.val


    def addAtHead(self, val):
        """
        :type val: int
        :rtype: None
        """
        #这个是实现从头部节点加入数据的代码部分
        # p=ListNode(val)
        # p.next=self.head.next
        # self.head.next=p
        # self.size+=1
		
		#这个是调用类内已经写好的addAtIndex函数
        self.addAtIndex(0,val)


    def addAtTail(self, val):
        """
        :type val: int
        :rtype: None
        """
        #这个是实现从尾部节点加入数据的代码部分
        # cur=self.head
        # p=ListNode(val)
        # p.next=None
        # for i in range(self.size):
        #     cur=cur.next

        # cur.next=p
        # self.size+=1
        
        #这个是调用类内已经写好的addAtIndex函数
        self.addAtIndex(self.size,val)

    def addAtIndex(self, index, val):
        """
        :type index: int
        :type val: int
        :rtype: None
        """
        if index<0 or index>self.size:
            return -1
        cur=self.head
        for i in range(index):
            cur=cur.next
        p=ListNode(val)
        p.next=cur.next
        cur.next=p
        self.size+=1



    def deleteAtIndex(self, index):
        """
        :type index: int
        :rtype: None
        """
        if index<0 or index>=self.size:
            return -1
        
        cur=self.head
        for i in range(index):
            cur=cur.next
        cur.next=cur.next.next
        self.size-=1

反转链表

自己实现的

python 复制代码
        pre=None
        cur=head
        if cur==None: return head
        tmp=cur.next
        while tmp:
            cur.next=pre
            pre=cur
            cur=tmp
            tmp=tmp.next
        cur.next=pre
        return cur

参照代码随想录更改的

python 复制代码
 		pre=None
        cur=head
        while cur:
            tmp=cur.next
            cur.next=pre
            pre=cur
            cur=tmp
   
        return pre

两两交换链表中的节点

我的垃圾代码,拼拼凑凑

python 复制代码
  		dummy_head = ListNode(next = head)
        pre=dummy_head
        if dummy_head.next==None: return head
        cur=pre.next
        beh=cur.next
        while beh:
            tmp=beh.next
            pre.next=beh
            beh.next=cur
            cur.next=tmp

            pre=cur
            cur=tmp
            if tmp!=None:
                beh=tmp.next
            else:
                beh=None

        
        return dummy_head.next

删除链表中的倒数第N个节点

python 复制代码
 		dummy=ListNode(next=head)
        slow=dummy
        fast=dummy
        for i in range(n):
            fast=fast.next
        while fast.next:
            fast=fast.next
            slow=slow.next
        slow.next=slow.next.next
        return dummy.next 

链表相交

python 复制代码
		p1,p2=headA,headB
        Asize,Bsize=0,0
        while p1:
            p1=p1.next
            Asize+=1
        while p2:
            p2=p2.next
            Bsize+=1
        if Asize-Bsize>=0:
            p1,p2=headB,headA
            Asize,Bsize=Bsize,Asize
        else:
            p1,p2=headA,headB
        for _ in range(Bsize-Asize):
            p2=p2.next
        while p1:
            if p1==p2:
                return p1
            p1=p1.next
            p2=p2.next
        return None

环形链表II

python 复制代码
		fast,slow=head,head
        while fast and fast.next:
            fast=fast.next.next
            slow=slow.next
            if slow==fast:
                x1=head
                x2=slow
                while x1!=x2:
                    x1=x1.next
                    x2=x2.next
                return x1
相关推荐
疋瓞3 小时前
python和C++对比(1)_数据类型和数据结构
数据结构·c++·python
如此这般英俊4 小时前
手搓Claude Code-第六章 subagent
数据结构·人工智能·python·语言模型·自然语言处理
元Y亨H5 小时前
Python - FastAPI 全方位介绍
python·fastapi
Tairitsu_H5 小时前
[LC优选算法#17] 链表 | 合并 K 个升序链表 | K个⼀组翻转链表
数据结构·算法·链表
error:(6 小时前
【系统与实战双精通】VS Code 调试 ROS2 Python 节点与 Launch 系统指南
android·java·python
weigangwin6 小时前
LlamaIndex 第一次试用:别先写 RAG Demo,先验上下文合同
python·ai·agent·rag·检索·llamaindex·观测性
省四收割者6 小时前
一文详解信号完整性(1)
python·嵌入式硬件·数学建模·信息与通信·信号处理·智能硬件
ai生成式引擎优化技术7 小时前
从知识连接到智能组织:WSaiOS认知网络的理论框架与系统设计摘要
网络·python·plotly·django·pygame
用户8356290780517 小时前
使用 Python 保护和取消保护 Excel 工作表和工作簿
后端·python
zwenqiyu7 小时前
非线性字符串数据结构串讲
数据结构·c++·学习·算法