82. Delete the sort the list of repeating elements II
82. In the Remove Duplicates from the Sorted List II
Title Description
Given a sorted list, delete all of the nodes contain repeated digits, leaving only the digital original list is not recurring.
LeetCode82. Remove Duplicates from Sorted List II中等
Example 1:
Input:
1-> 2-> 3-> 3-> 4-> 4-> 5
Output: 1-> 2-> 5
Output: 1-> 2-> 5
Example 2:
Input:
1-> 1-> 1-> 2-> 3
Output: 2-> 3
Output: 2-> 3
Java implementation
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
@Override
public String toString() {
return val + "->" + next;
}
}
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode t = new ListNode(0);
t.next = head;
ListNode p = t;
while (p.next != null && p.next.next != null) {
if (p.next.val == p.next.next.val) {
int dup = p.next.val;
while (p.next != null && p.next.val == dup) {
p.next = p.next.next;
}
} else {
p = p.next;
}
}
return t.next;
}
}
Similar topics
Reference material