-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from atulgoel126/main-q82
Added solution for Q82 - Remove dupes from sorted list, updated README
- Loading branch information
Showing
3 changed files
with
54 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
33 changes: 30 additions & 3 deletions
33
src/main/java/leetcode/linked_list/q82/RemoveDuplicatesfromSortedListII.java
This file contains 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 |
---|---|---|
@@ -1,6 +1,33 @@ | ||
package leetcode.linked_list.q82; | ||
|
||
import leetcode.commons.ListNode; | ||
|
||
public class RemoveDuplicatesfromSortedListII { | ||
// public ListNode deleteDuplicates(ListNode head) { | ||
// return null; | ||
// } | ||
public ListNode deleteDuplicates(ListNode head) { | ||
|
||
if (head == null || head.next == null) { | ||
return head; | ||
} | ||
|
||
ListNode dummy = new ListNode(-1); | ||
dummy.next = head; | ||
|
||
ListNode prev = dummy; | ||
ListNode curr = head; | ||
|
||
// -1-> 1 -> 1 -> 1 -> 2 -> 3 -> 3 | ||
// p c c.n | ||
while (curr != null && curr.next != null) { | ||
if (curr.val == curr.next.val) { | ||
while (curr.next != null && curr.val == curr.next.val) { | ||
curr = curr.next; | ||
} | ||
prev.next = curr.next; | ||
} else { | ||
prev = prev.next; | ||
} | ||
curr = curr.next; | ||
} | ||
return dummy.next; | ||
} | ||
} |
This file contains 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