관리 메뉴

솜씨좋은장씨

[leetCode] 83. Remove Duplicates from Sorted List (Python) 본문

Programming/코딩 1일 1문제

[leetCode] 83. Remove Duplicates from Sorted List (Python)

사용자 솜씨좋은장씨 2020. 12. 1. 16:56

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

Solution

# 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: ListNode) -> ListNode:
        list_values = []
        
        while head != None:
            list_values.append(head.val)
            
            head = head.next
            
        set_values = sorted(list(set(list_values)))
            
        answerList = ListNode(0) 
        
        result = answerList 
        
        for num in set_values: 
            answerList.next = ListNode(num) 
            answerList = answerList.next 
        return result.next

 

SOMJANG/CODINGTEST_PRACTICE

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

github.com

0 Comments
댓글쓰기 폼