Skip to content

Latest commit

 

History

History
43 lines (33 loc) · 732 Bytes

206.md

File metadata and controls

43 lines (33 loc) · 732 Bytes

Remove Linked List Elements

Description

link


Solution

See Code


Code

O(n)

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

class Solution:
    def removeElements(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        cur = head
        pre = res = ListNode(0)
        pre.next = cur
        while cur:
            if cur.val == val:
                pre.next = cur.next
            else:
                pre = cur
            cur = cur.next
        return res.next