LeetCode/Top Interview 150

82. Remove Duplicates from Sorted List II

hyunkookim 2024. 12. 9. 18:26

82. Remove Duplicates from Sorted List II

 

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(0, head)

        left = dummy

        while left:
            if left.next and left.next.next and (left.next.val == left.next.next.val):
                temp = left.next.next
                while temp and temp.next and (temp.val == temp.next.val): # 같으면 계속 옮김
                    temp = temp.next
                left.next = temp.next # 중간 동일한 값들 모두 삭제
            else:
                left = left.next
                
        return dummy.next

 

 

https://youtu.be/ff6LbGhd1AU?si=eg-XK410O_RxrPp1

 

'LeetCode > Top Interview 150' 카테고리의 다른 글

86. Partition List  (0) 2024.12.10
61. Rotate List  (0) 2024.12.10
19. Remove Nth Node From End of List  (1) 2024.12.09
25. Reverse Nodes in k-Group  (0) 2024.12.09
92. Reverse Linked List II  (1) 2024.12.07