-
Notifications
You must be signed in to change notification settings - Fork 20
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
mrid88
wants to merge
2
commits into
Dijkstra-Edu:master
Choose a base branch
from
mrid88:feature/remove
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
Problems/#83 - Remove Duplicates from Sorted List - Easy/Explanation.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
15 changes: 15 additions & 0 deletions
15
Problems/#83 - Remove Duplicates from Sorted List - Easy/Solution.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Explain the complexity!