subject : Given the head pointer of one-way linked list and the value of a node to be deleted , Define a function to delete the node . Return the head node of the deleted linked list . Be careful : This question is different from the original one
Example 1:
Input : head = [4,5,1,9], val = 5 Output : [4,1,9]
explain : Given that the value of your list is 5 Second node of , So after calling your function , The list should be 4 -> 1 -> 9.
Example 2:Input : head = [4,5,1,9], val = 1 Output : [4,5,9]
explain : Given that the value of your list is 1 The third node of , So after calling your function , The list should be 4 -> 5 -> 9.
Program description :
All the code :
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if head.val == val:
return head.next
pre = head
cur = pre.next
while cur:
if cur.val == val:
pre.next = cur.next
return head
pre = cur
cur = cur.next
return head
Title source : Power button (LeetCode)
Dream morning From the Aofei t