/**
* 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 prev = head, curr = head.next;
for( ; curr != null; curr = curr.next) {
if(curr.val != prev.val) {
prev.next = curr;
prev = curr;
}
}
prev.next = null;
return head;
}
}
Time: O(n)
Space: O(1)