83. Remove Duplicates from Sorted List

Doyeon Kim·2022년 6월 3일

코딩테스트 공부

목록 보기
74/171

문제 링크 : https://leetcode.com/problems/remove-duplicates-from-sorted-list/


정렬된 리스트에서 겹치는 숫자들이 있을떄

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

노드를 탐색하면서 현재노드와 다음 노드의 값이 같은경우 현재노드의 다음노드를 다다음노드로 연결하고 당겨주고 아닐 경우 커서를 앞으로 한칸 이동시킴

Runtime: 105 ms, faster than 5.14% of Python3 online submissions for Remove Duplicates from Sorted List.
Memory Usage: 13.8 MB, less than 97.78% of Python3 online submissions for Remove Duplicates from Sorted List.

profile
성장하고 도전하는 개발자. 프로그래밍 좋아하세요?

0개의 댓글