leetcode#203 Remove Linked List Elements

정은경·2022년 6월 18일
0

알고리즘

목록 보기
91/125

1. 문제

2. 나의 풀이

# 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: ListNode
        :type val: int
        :rtype: ListNode
        """
        
        prev_node = None
        current_node = head
        while current_node:
            if prev_node and current_node.val == val:
                prev_node.next = current_node.next
            if not prev_node and current_node.val == val:
                head = current_node.next
            if current_node.val != val:
                prev_node = current_node
            current_node = current_node.next
        
        return head

3. 남의 풀이

profile
#의식의흐름 #순간순간 #생각의스냅샷

0개의 댓글