数据结构基础——链表相关操作及应用(含python代码,更新中)



数据结构算法相关代码集合见https://github.com/lankuohsing/DataStructureInPython ,欢迎fork和star

众所周知,计算机中的数据结构底层无非是链表(linked list)或者线性表(linear list)。因此,掌握这些基本的数据结构的结构和常用操作是很重要的。本文我们来介绍一下链表的常用操作,主要采用经典算法题的形式来加以呈现说明

0. 链表的数据结构描述

链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的,也即下一个节点的位置是同过上一个节点的next指针来确定的。由于这个特点,链表有以下几个优点:

  • 在创建时不需要知道数据的总大小,因此可以充分利用计算机的内存空间。
  • 在对链表进行插入和删除节点的操作时,时间复杂度可以达到O(1)
    同时也有以下几个缺点:
  • 每个节点需要额外存储指针,空间消耗较大
  • 查找某个元素的时间复杂度为O(n)

1. 常见操作

1.1 删除链表中的结点

leetcode-237 237. 删除链表中的节点
请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点。传入函数的唯一参数为 要被删除的节点

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        node.val=node.next.val
        node.next=node.next.next

关键点:将该节点后继节点的数据拷贝到当前节点,并且删除后继节点

leetcode-203 203. 移除链表元素
给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        if head is None:
            return head
        while head is not None and head.val==val:
            head=head.next

        if head is None:
            return head
        cur=head
        while cur.next is not None:
            if cur.next.val==val:
                cur.next=cur.next.next
            else:
                cur=cur.next
        return head

关键点:用一个指针cur从头到后遍历,如果该指针的下一个节点非空并且值等于目标值,则cur的next指向下下个节点(也即删除了下个节点),否则cur往后移动。直到cur的下个节点为空(到了末尾)。需要注意的是,可能cur一开始就指向了目标节点(也即head就是目标节点),这时就要移动cur到下一个节点。

leetcode-83 83. 删除排序链表中的重复元素
存在一个按升序排列的链表,给你这个链表的头节点 head ,请你删除所有重复的元素,使每个元素 只出现一次

返回同样按升序排列的结果链表。

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if head is None:
            return head
        cur=head
        while cur.next is not None:
            if cur.val==cur.next.val:
                cur.next=cur.next.next
            else:
                cur=cur.next
        return head

关键点:用一个cur从头开始往后遍历,如果cur的后继结点非空,且后继结点的值等于后继的后继的值,则cur指向后继的后继(删除第一个重复的值),否则cur往后移动。知道cur变为空,此时返回head

1.2 合并有序链表

leetcode-21 21. 合并两个有序链表
将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        if l1 is None:
            return l2
        if l2 is None:
            return l1
        start=None
        cur=None
        if l2.val <= l1.val:
            cur=l2
            l2=l2.next
        else:
            cur=l1
            l1=l1.next
        start=cur
        while l1 is not None and l2 is not None:
            if l2.val <= l1.val:
                cur.next=l2
                l2=l2.next
            else:
                cur.next=l1
                l1=l1.next
            cur=cur.next
        if l1 is not None:
            cur.next=l1
        else:
            cur.next=l2
        return start

关键点:l1和l2分别往后逐节点移动并比较大小,利用一个指针cur来指向l1和l2中较小的节点,并将cur往后移动。当l1或l2有一个为None时,迭代停止, 不为空的那个节点开始的尾巴直接连在cur后面。要注意的是,需要一个start节点来指向起点。

1.3. 反转链表

leetcode-206 206. 反转链表
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if head is None:
            return head
        p1=head
        p2=head.next
        while p2 is not None:
            head.next=p2.next
            p2.next=p1
            p1=p2
            p2=head.next

        return p1

关键点:用两个指针p1和p2,一前一后,每次迭代时head指向p2的后继结点,p2指向p1,p1移动到p2,p2移动到下一个节点(借助head)。

1.4. 带环的链表

leetcode-141 141. 环形链表
给定一个链表,判断链表中是否有环。

# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def hasCycle(self, head: ListNode) -> bool:
        fast=head
        while fast is not None:
            head=head.next
            if head is None:
                return False
            fast=fast.next
            if fast is None:
                return False
            fast=fast.next
            if fast==head:
                return True
        return False

关键点:两个指针同时从head开始出发,一个指针每次走一步,另一个指针每次走两步,如果会相遇(两个指针相等)则说明有环。注意循环的跳出条件,只要有一个指针为None就应该跳出且返回False

leetcode-142 142. 环形链表 II
给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(self, head: ListNode) -> ListNode:
        fast=head
        slow=head
        meet=None
        is_meet=False
        while slow is not None:
            slow=slow.next
            if slow is None:
                return None
            fast=fast.next
            if fast is None:
                return None
            fast=fast.next
            if fast is None:
                return None
            if fast==slow:
                meet=slow
                is_meet=True
                break
        if is_meet:
            slow=head
            fast=meet
            while slow!=fast:
                slow=slow.next
                fast=fast.next
            return slow
        return None

具体分析过程见本人另一篇博文:https://blog.csdn.net/thuchina/article/details/77808522

2. 链表的应用

2.1. 利用链表构建HashSet

leetcode-705 705. 设计哈希集合

class MyHashSet:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.base=769
        self.data=[[]]*self.base
    def hash(self,key:int,base:int):
        return key%base

    def add(self, key: int) -> None:
        index=self.hash(key,self.base)
        for e in self.data[index]:
            if e==key:
                return
        self.data[index].append(key)
        return



    def remove(self, key: int) -> None:
        index=self.hash(key,self.base)
        for i in range(0,len(self.data[index])):
            if self.data[index][i]==key:
                self.data[index].pop(i)
                return

    def contains(self, key: int) -> bool:
        """
        Returns true if this set contains the specified element
        """
        index=self.hash(key,self.base)
        for i in range(0,len(self.data[index])):
            if self.data[index][i]==key:
                return True
        return False



# Your MyHashSet object will be instantiated and called as such:
# obj = MyHashSet()
# obj.add(key)
# obj.remove(key)
# param_3 = obj.contains(key)

关键点:先设定一个素数作为对输入(key)求模运算的基,以这个素数为长度创建一个数组,数组中每个位置(求模的结果作为序号的位置)存一个链表,链表存的是对素数求模后结果一样的输入值(key)。

2.2.利用链表构建HashMap

leetcode-706 706. 设计哈希映射

class MyHashMap:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.base=769
        self.data=[[]]*self.base
    def hash(self,key:int,base:int):
        return key%base


    def put(self, key: int, value: int) -> None:
        """
        value will always be non-negative.
        """
        index=self.hash(key,self.base)
        for i in range(0,len(self.data[index])):
            if self.data[index][i][0]==key:
                self.data[index][i]=(key,value)
                return
        self.data[index].append((key,value))


    def get(self, key: int) -> int:
        """
        Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key
        """
        index=self.hash(key,self.base)
        for i in range(0,len(self.data[index])):
            if self.data[index][i][0]==key:
                return self.data[index][i][1]
        return -1


    def remove(self, key: int) -> None:
        """
        Removes the mapping of the specified value key if this map contains a mapping for the key
        """
        index=self.hash(key,self.base)
        for i in range(0,len(self.data[index])):
            if self.data[index][i][0]==key:
                self.data[index].pop(i)
                return



# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)

关键点:与前面的HashSet类似,只不过存的是<key,value>。定位仍然是用key来定位

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
哈希表是一种高效的数据结构,可以用来存储和查找键值对。其,哈希函数将键映射到一个特定的桶,每个桶存储一组键值对。在哈希表,如果两个键被映射到同一个桶,就会发生碰撞。为了解决这个问题,可以使用链表法。 链表法是一种解决哈希表碰撞问题的方法。具体来说,对于哈希表的每个桶,可以使用一个链表来存储所有映射到该桶的键值对。如果发生碰撞,只需要将新的键值对添加到链表的末尾即可。 下面是一个使用链表法实现哈希表的示例代码: ```python class Node: def __init__(self, key, value): self.key = key self.value = value self.next = None class HashTable: def __init__(self, capacity): self.capacity = capacity self.buckets = [None] * capacity def hash_function(self, key): return hash(key) % self.capacity def put(self, key, value): index = self.hash_function(key) node = self.buckets[index] while node: if node.key == key: node.value = value return node = node.next new_node = Node(key, value) new_node.next = self.buckets[index] self.buckets[index] = new_node def get(self, key): index = self.hash_function(key) node = self.buckets[index] while node: if node.key == key: return node.value node = node.next return None def remove(self, key): index = self.hash_function(key) node = self.buckets[index] prev = None while node: if node.key == key: if prev: prev.next = node.next else: self.buckets[index] = node.next return prev = node node = node.next ``` 在这个示例,我们定义了一个Node类来表示哈希表的每个节点,每个节点包一个键、一个值和一个指向下一个节点的指针。我们还定义了一个HashTable类来实现哈希表,其一个桶数组和一些基本的操作方法,如put、get和remove。 在put方法,我们首先使用哈希函数计算出键的索引,然后遍历桶链表,查找该键是否已经存在于哈希表。如果找到了该键,我们只需要更新其对应的值即可。否则,我们创建一个新的节点,并将其添加到链表的开头。 在get方法,我们同样使用哈希函数计算出键的索引,然后遍历桶链表,查找该键的值。如果找到了该键,我们返回其对应的值。否则,返回None。 在remove方法,我们首先使用哈希函数计算出键的索引,然后遍历桶链表,查找该键。如果找到了该键,我们将其从链表删除即可。 总的来说,链表法是一种简单且常用的哈希表解决碰撞问题的方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值