Post

leetcode(리트코드)-203 Remove Linked List Elements(python)

leetcode 203 - Remove Linked List Elements 문제입니다.

1. 문제

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


2. Input , Output


3. 분류 및 난이도

Eazy 난이도 문제입니다.


4. 문제 해석

  • 링크드 리스트가 주어지는데, val로 들어온 값을 제거한 링크드 리스트를 리턴하세요.

5. code

코드설명

python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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: ListNode, val: int) -> ListNode:
        def solve(head,val):
            #범위를 벗어나지 않게.
            if head and head.next : 
                if head.next.val == val :
                    if head.next.next == None :
                        head.next = None
                    else:
                        head.next = head.next.next
                    solve(head,val)
                else : 
                    solve(head.next,val)
        #처음 들어온 값들이 val과 같은 값인 경우.
        while head and head.val == val : 
            head = head.next
        res = head
        solve(head,val)
        return res
    
    

6. 결과 및 후기, 개선점

필요시 c++로 짜드리겠습니다.

This post is licensed under CC BY 4.0 by the author.