Skip to content

Solution #83 - Mridul/Edited - 16/03/2025 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Explanation of the `deleteDuplicates` Code

This Java class provides a solution to remove duplicate values from a sorted singly linked list. It ensures that each value appears only once in the resulting list.

---

## Class: `Solution`

### Method: `deleteDuplicates(ListNode head)`

#### **Purpose**
The method removes consecutive duplicate values from a sorted linked list.

#### **Parameters**
- `head`: The head node of the input linked list.

#### **Logic**
1. **Initialization**:
- A pointer `current` is initialized to point to the `head` of the linked list.

2. **Iterative Traversal**:
- While `current` is not `null` and `current.next` is not `null`:
1. Check if the current node's value `current.val` is equal to the next node's value `current.next.val`:
- If true, skip the next node by updating `current.next` to `current.next.next` (removing the duplicate).
- If false, move the `current` pointer to the next node (`current = current.next`).

3. **Return the Updated List**:
- Once all duplicates are removed, return the modified `head` of the linked list.

---

### Example Execution
#### Input:
A sorted linked list: `1 -> 1 -> 2 -> 3 -> 3`

#### Output:
The updated linked list: `1 -> 2 -> 3`

---

### Complexity Analysis
1. **Time Complexity**:
- \(O(n)\): The list is traversed once, where \(n\) is the number of nodes.

2. **Space Complexity**:
- \(O(1)\): No additional data structures are used. The operation modifies the list in place.

---

### Key Points
- This solution works only for **sorted linked lists**. If the input list is unsorted, the duplicates will not be removed correctly.
- The approach uses in-place modification, making it efficient in terms of space.

---

### Summary
The `deleteDuplicates` method is a simple yet efficient way to clean up a sorted linked list by removing consecutive duplicate nodes, preserving only unique values.

Time Complexity: O(n)
Space Complexity: O(1)
Comment on lines +59 to +60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain the complexity!

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode current = head;

while (current != null && current.next != null) {
if (current.val == current.next.val) {
current.next = current.next.next;
} else {
current = current.next;
}
}

return head;
}
}