Description of the original title:
Deletes all nodes in the linked list val
that are equal to the given value.
Have you ever encountered this problem in a real interview? Yes
Sample Example
Given the list of links, 1->2->3->3->4->5->3
and val = 3
, you need to return the list after deleting 3: 1->2->4->5
.
labelLinked list
Topic Analysis:
Deletes all nodes in the linked list val
that are equal to the given value.
Iterate through the linked list and find the node where Next.val equals Val, delete.
Note that all elements in the list may be equal to Val, the beginning of the loop needs to be deleted from the header, and a new head node is required.
Source:
# Definition for singly-linked list.# class listnode:# def __init__ (self, x): # self.val = x# Self.next = Nonec Lass Solution: # @param head, a listnode # @param val, an integer # @return a listnode def removeelements ( Self, head, val): # Write Your code this if head is none: return None # all elements in the list may be equal to Val, so a new head node is required. c11/>new = ListNode (0) New.next = head head = new pre = Head # traverse list, delete all nodes equal to Val while Pre.next is not None: if Pre.next.val = = val: pre.next = pre.next.next else: pre = Pre.next return New.next
Lintcode Python Simple class topic 452. Remove elements from a linked list