【代码随想录】链表

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
相关推荐
&梧桐树夏1 分钟前
【算法系列-链表】删除链表的倒数第N个结点
数据结构·算法·链表
QuantumStack5 分钟前
【C++ 真题】B2037 奇偶数判断
数据结构·c++·算法
wclass-zhengge1 小时前
数据结构篇(绪论)
java·数据结构·算法
Dylanioucn1 小时前
【分布式微服务云原生】探索Redis:数据结构的艺术与科学
数据结构·redis·分布式·缓存·中间件
何事驚慌1 小时前
2024/10/5 数据结构打卡
java·数据结构·算法
结衣结衣.1 小时前
C++ 类和对象的初步介绍
java·开发语言·数据结构·c++·笔记·学习·算法
大三觉醒push亡羊补牢女娲补天版1 小时前
数据结构之排序(5)
数据结构
TJKFYY1 小时前
Java.数据结构.HashSet
java·开发语言·数据结构
杰哥在此1 小时前
Python知识点:如何使用Multiprocessing进行并行任务管理
linux·开发语言·python·面试·编程
卡皮巴拉吖1 小时前
【堆排】为何使用向下调整法建堆比向上调整法建堆更好呢?
数据结构