Python实现:
这个题目十分简单,主要是要用到穿针引线以及特殊考虑以下情况:
1.链表的下一个node是否为空,为空的话则不能和val进行比较
2.链表的第一个node,也就是head所在的position是否是需要进行删除的节点
3.需要删除的链表node是不是最后一个节点,需要单独考虑
4.对正常情况下的链表进行删除
这些情况均需要使用if else语句进行排除
代码如下所示:
# 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 cur=head while head and head.next: if head.next.val==val: if head.next.next==None: head.next=None return cur else: head.next=head.next.next if head.val==val: return head.next head=head.next return cur