알고리즘/LeetCode

[LeetCode/리트코드] #203. Remove Linked List Elements [큐/queue/python/파이썬]

https://leetcode.com/problems/remove-linked-list-elements/

 

Remove Linked List Elements - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
        node = ListNode(None)
        node.next = head
        head = node
        while head.next:
            if head.next.val==val:
                head.next=head.next.next
            else:
                head = head.next
        return node.next