Skip to content

Commit

Permalink
problem: 0082 remove duplicates from sorted list
Browse files Browse the repository at this point in the history
  • Loading branch information
cychiu8 committed Jan 26, 2022
1 parent 933f4e9 commit f00aaaf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# 0082. Remove Duplicates from Sorted List II

* Difficulty: medium
* Link: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
* Topics: Linked-List, Multiple-Pointers

# Clarification

1. Check the inputs and outputs
- INPUT: Linked List
- OUTPUT: Linked List

# Naive Solution

### Thought Process

- current pointer and previous pointer
1. iterate the linked list
- if current.val == current.next.val: prev.next=current.next
- else
- previous = previous.next
- current = current.next

![Untitled](./Untitled.png)

- Implement

```python
**# 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:
dummy = ListNode(0, head)
prev = dummy
current = dummy.next
while current:
if current.next and current.val == current.next.val:
while current.next and current.val == current.next.val:
current = current.next
prev.next = current.next
else:
prev = prev.next
current = current.next
return dummy.next**
```


### Complexity

- Time complexity:$O(n)$
- Space complexity:$O(1)$

### Problems & Improvement

-
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f00aaaf

Please sign in to comment.