首页 > 试题广场 >

删除链表的节点

[编程题]删除链表的节点
  • 热度指数:65545 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解

给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。返回删除后的链表的头节点。

1.此题对比原题有改动
2.题目保证链表中节点的值互不相同
3.该题只会输出返回的链表和结果做对比,所以若使用 C 或 C++ 语言,你不需要 free 或 delete 被删除的节点

数据范围:
0<=链表节点值<=10000
0<=链表长度<=10000
示例1

输入

{2,5,1,9},5

输出

{2,1,9}

说明

给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 2 -> 1 -> 9   
示例2

输入

{2,5,1,9},1

输出

{2,5,9}

说明

给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 2 -> 5 -> 9   

说明:本题目包含复杂数据结构ListNode,点此查看相关信息
class Solution:
    def deleteNode(self , head: ListNode, val: int) -> ListNode:
        # write code here
        pre = dummy = ListNode(0)
        pre.next = head 
        while head:
            if head.val == val:
                pre.next = head.next 
                return dummy.next 
            pre = head 
            head = head.next 

发表于 2023-04-27 14:51:11 回复(0)
class Solution:
    def deleteNode(self , head: ListNode, val: int) -> ListNode:
        # write code here
        if not head: return None
        if head.val == val:
            return head.next
        else:
            p1 = head
            p2 = head.next
            while p2:
                if p2.val == val:
                    p1.next = p2.next
                    return head
                else:
                    p1 = p1.next
                    p2 = p2.next
            return None
疑问:倒数第五行
return head
为什么对p1做改动会影响head???
发表于 2023-03-08 21:55:22 回复(1)
class Solution:
    def deleteNode(self , head: ListNode, val: int) -> ListNode:
        # write code here
        if head is None:
            return None
        if head.val == val:
            return head.next
        pre, cur = head, head.next
        while cur:
            if cur.val == val:
                pre.next = cur.next
                break
            else:
                pre = cur
                cur = cur.next
        return head
发表于 2022-03-14 13:19:27 回复(0)
哨兵节点+快慢指针

class Solution:
    def deleteNode(self , head: ListNode, val: int) -> ListNode:
        # write code here
#         if not head: return None
        
        # 哨兵节点:主要是为了简化 如果[删除的是第一个,需要单独处理的情况]
        sentinel = ListNode(0)
        sentinel.next = head

        # 快慢指针
        fast,slow = head, sentinel
        while fast:
            if fast.val == val:
                slow.next = fast.next # 删除:即跳过该节点
                # 若val不存在重复,只有一个去删除,
                # return sentinal.next 
            else:  # 必须用else,如果考虑删除val的多次出现
                slow = slow.next
            fast = fast.next

        return sentinel.next


发表于 2021-11-08 01:16:32 回复(0)

python 双指针

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param head ListNode类 
# @param val int整型 
# @return ListNode类
#
class Solution:
    def deleteNode(self , head: ListNode, val: int) -> ListNode:
        # write code here
        if not head:return
        if head.val==val:return head.next
        pre,cur=head,head.next
        while cur and cur.val!=val:
            pre,cur=cur,cur.next
        if cur:pre.next=cur.next
        return head
发表于 2021-11-04 15:37:20 回复(1)