문제
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
풀이
정렬된 단일연결리스트를 순회하며 중복(동일값) 여부를 확인하여 제거한다.
class Solution:
def deleteDuplicates(self, head: ListNode) -> ListNode:
cur = head
while cur :
if cur.next and cur.val == cur.next.val :
cur.next = cur.next.next
else :
cur = cur.next
return head
'알고리즘 트레이닝 > LeetCode' 카테고리의 다른 글
88. Merge Sorted Array (0) | 2020.05.11 |
---|---|
21. Merge Two Sorted Lists (0) | 2020.05.09 |
70. Climbing Stairs (0) | 2020.05.08 |
69. Sqrt(x) (0) | 2020.05.08 |
58. Length of Last Word (0) | 2020.05.08 |