Question
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
Stats
Frequency | 3 |
Difficulty | 1 |
Adjusted Difficulty | 1 |
Time to use | -------- |
Ratings/Color = 1(white) 2(lime) 3(yellow) 4/5(red)
Analysis
This question is trivial. The next following up question is not easy.
Code
my code
public ListNode deleteDuplicates(ListNode head) {
if (head == null) return null;
ListNode pre = head;
ListNode post = head;
while (post != null){
post = post.next;
if (post == null || pre.val != post.val){
pre.next = post;
pre = post;
}
}
return head;
}