Skip to content

Commit a05b0a8

Browse files
authored
Merge pull request #527 from Vipullakum007/remove-from-sorted-list-solution
Remove Duplicates from sorted list solution added
2 parents 437e69c + 3dd9af1 commit a05b0a8

File tree

5 files changed

+1423
-2
lines changed

5 files changed

+1423
-2
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
---
2+
id: remove-duplicates-from-sorted-list
3+
title: Remove Duplicates from Sorted List Solution
4+
sidebar_label: 0083 Remove Duplicates from Sorted List
5+
tags:
6+
- Linked List
7+
- Two Pointers
8+
- LeetCode
9+
- Python
10+
- Java
11+
- C++
12+
description: "This is a solution to the Remove Duplicates from Sorted List problem on LeetCode."
13+
---
14+
15+
## Problem Description
16+
17+
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
18+
19+
### Examples
20+
21+
**Example 1:**
22+
```
23+
Input: head = [1,1,2]
24+
Output: [1,2]
25+
```
26+
27+
**Example 2:**
28+
29+
```
30+
Input: head = [1,1,2,3,3]
31+
Output: [1,2,3]
32+
```
33+
34+
### Approach for Removing Duplicates from Sorted List
35+
36+
**Intuition:**
37+
The problem requires removing duplicates from a sorted singly-linked list. The approach involves iterating through the list and removing duplicates while maintaining the sorted order.
38+
39+
**Approach:**
40+
- Initialize a pointer `current` to the head of the linked list to traverse the list.
41+
- Start a `while` loop that continues until `current` reaches the end of the list or `current.next` reaches null.
42+
- Inside the loop, compare the value of the current node `current.val` with the value of the next node `current.next.val`.
43+
- If the values are equal, it indicates a duplicate node. In this case, update the next pointer of the current node `current.next` to skip the next node (remove the duplicate).
44+
- If the values are not equal, move the `current` pointer to the next node, continuing the traversal.
45+
- Repeat the loop until the end of the list is reached, ensuring that all duplicates are removed while maintaining the sorted order of the remaining nodes.
46+
- After the loop, return the modified linked list, which contains no duplicates.
47+
48+
49+
### Code in Different Languages
50+
51+
#### Java (code):
52+
53+
```java
54+
class Solution {
55+
public ListNode deleteDuplicates(ListNode head) {
56+
ListNode current = head;
57+
58+
while (current != null && current.next != null) {
59+
if (current.val == current.next.val) {
60+
current.next = current.next.next;
61+
} else {
62+
current = current.next;
63+
}
64+
}
65+
66+
return head;
67+
}
68+
}
69+
```
70+
71+
#### Python (code)
72+
73+
```python
74+
class Solution:
75+
def deleteDuplicates(self, head: ListNode) -> ListNode:
76+
current = head
77+
78+
while current and current.next:
79+
if current.val == current.next.val:
80+
current.next = current.next.next
81+
else:
82+
current = current.next
83+
84+
return head
85+
```
86+
87+
#### CPP (code) ;
88+
89+
```cpp
90+
class Solution {
91+
public:
92+
ListNode* deleteDuplicates(ListNode* head) {
93+
ListNode* current = head;
94+
95+
while (current && current->next) {
96+
if (current->val == current->next->val) {
97+
current->next = current->next->next;
98+
} else {
99+
current = current->next;
100+
}
101+
}
102+
103+
return head;
104+
}
105+
};
106+
```
107+
108+
#### Complexity Analysis
109+
110+
- **Time Complexity:** O(n)
111+
112+
- The algorithm iterates through the linked list once, where n is the number of nodes in the list. Each node is examined once to identify and remove duplicates.
113+
114+
- **Space Complexity:** O(1)
115+
- The algorithm uses a constant amount of additional memory space for variables, regardless of the size of the input linked list, making its space complexity constant.
116+
117+
## References
118+
119+
- **LeetCode Problem**: [Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)
120+
- **Solution Link**: [LeetCode Solution](https://leetcode.com/problems/balanced-binary-tree/solution/)
121+
- **Authors GeeksforGeeks Profile:** [Vipul lakum](https://leetcode.com/u/vipul_lakum_02/)

0 commit comments

Comments
 (0)