Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 프로그래머스
- Docker
- ChatGPT
- 편스토랑
- 맥북
- Git
- 우분투
- AI 경진대회
- PYTHON
- leetcode
- 금융문자분석경진대회
- Real or Not? NLP with Disaster Tweets
- Baekjoon
- programmers
- Kaggle
- 프로그래머스 파이썬
- github
- 백준
- dacon
- SW Expert Academy
- 캐치카페
- 더현대서울 맛집
- hackerrank
- 편스토랑 우승상품
- 코로나19
- ubuntu
- gs25
- 파이썬
- 데이콘
- 자연어처리
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 82. Remove Duplicates from Sorted List II (Python) 본문
Programming/코딩 1일 1문제
[leetCode] 82. Remove Duplicates from Sorted List II (Python)
솜씨좋은장씨 2020. 12. 2. 00:09728x90
반응형
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Return the linked list sorted as well.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:
Input: 1->1->1->2->3
Output: 2->3
Solution
from collections import Counter
# 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_nums = []
while head != None:
list_nums.append(head.val)
head = head.next
cnt_items = [ num[0] for num in Counter(list_nums).items() if num[1] == 1]
answerList = ListNode(0)
result = answerList
for num in cnt_items:
answerList.next = ListNode(num)
answerList = answerList.next
return result.next
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 1436. Destination City (Python) (0) | 2020.12.06 |
---|---|
[leetCode] 744. Find Smallest Letter Greater Than Target (Python) (0) | 2020.12.05 |
[leetCode] 83. Remove Duplicates from Sorted List (Python) (0) | 2020.12.01 |
[leetCode] 148. Sort List (Python) (0) | 2020.11.29 |
[leetCode] 468. Validate IP Address (Python) (0) | 2020.11.22 |
Comments