관리 메뉴

솜씨좋은장씨

[leetCode] 203. Remove Linked List Elements (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 203. Remove Linked List Elements (Python)

솜씨좋은장씨 2020. 9. 29. 15:43
728x90
반응형

Remove all elements from a linked list of integers that have value val.

 

Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

Solution

# 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:
        while head and head.val == val:
            head = head.next
        if not head:
            return head

        next_node = head
        while next_node and next_node.next:
            if next_node.next.val == val:
                next_node.next = next_node.next.next
            else:
                next_node = next_node.next

        return head

 

 

SOMJANG/CODINGTEST_PRACTICE

1일 1문제 since 2020.02.07. Contribute to SOMJANG/CODINGTEST_PRACTICE development by creating an account on GitHub.

github.com

 

Comments