Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9fd0ab5

Browse files
solves remove duplicate from sorted list II
1 parent 0f5017b commit 9fd0ab5

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
| 79 | [Word Search](https://leetcode.com/problems/word-search) | [![Java](assets/java.png)](src/WordSearch.java) | |
7676
| 80 | [Remove Duplicates from Sorted Array II](https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedArrayII.java) | |
7777
| 81 | [Search in Rotated Sorted Array II](https://leetcode.com/problems/search-in-rotated-sorted-array-ii) | [![Java](assets/java.png)](src/SearchInRotatedSortedArrayII.java) | |
78+
| 82 | [Remove Duplicates from Sorted List II](https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedListII.java) | |
7879
| 83 | [Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list) | [![Java](assets/java.png)](src/RemoveDuplicatesFromSortedList.java) [![Python](assets/python.png)](python/remove_duplicates_from_linked_list.py) | |
7980
| 88 | [Merge Sorted Array](https://leetcode.com/problems/merge-sorted-array) | [![Java](assets/java.png)](src/MergeSortedArray.java) [![Python](assets/python.png)](python/merge_sorted_array.py) | |
8081
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [![Java](assets/java.png)](src/BinaryTreeInorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_inorder_traversal.py) | |
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii
2+
// T: O(N)
3+
// S: O(1)
4+
5+
public class RemoveDuplicatesFromSortedListII {
6+
private static class ListNode {
7+
int val;
8+
ListNode next;
9+
}
10+
11+
public ListNode deleteDuplicates(ListNode head) {
12+
if (head == null) return null;
13+
ListNode tempHead = new ListNode();
14+
tempHead.next = head;
15+
int val;
16+
for (ListNode current = head, previous = tempHead ; current != null && current.next != null ; ) {
17+
if (current.val == current.next.val) {
18+
val = current.val;
19+
while (current.next != null && current.next.val == val) {
20+
current = current.next;
21+
}
22+
current = current.next;
23+
previous.next = current;
24+
} else {
25+
previous = current;
26+
current = current.next;
27+
}
28+
}
29+
return tempHead.next;
30+
}
31+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /