文章

203-remove-linked-list-elements

Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.

Input: head = [1,2,6,3,4,5,6], val = 6 Output: [1,2,3,4,5] Example 2:

Input: head = [], val = 1 Output: [] Example 3:

Input: head = [7,7,7,7], val = 7 Output: []

My solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def removeElements(self, head, val):
        """
        :type head: Optional[ListNode]
        :type val: int
        :rtype: Optional[ListNode]
        """
        # virtual head
        dummyhead = ListNode(val=0,next=head)
        curr = dummyhead 
        while curr.next:
            if curr.next.val == val:
                curr.next = curr.next.next
            else:
                curr = curr.next
        # dummyhead.next is the actural head
        return dummyhead.next
        
        

Time Complexity: O(n) Space Complexity: O(1)

本文由作者按照 CC BY 4.0 进行授权