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
.
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null)
return head;
ListNode current = head;
ListNode past = new ListNode(999);
int c = 9999;
while(current != null){
if(past.val == current.val ){
past.next = current.next;
current = current.next;
}
else{
past = current;
current = current.next;
}
}
return head;
}
}