LeetCode - Algorithms - 83. Remove Duplicates from Sorted List

Problem

83. Remove Duplicates from Sorted List

Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if (head==null)
return head;
ListNode node = head;
while(node!=null) {
if (node.next!=null && node.next.val==node.val) {
ListNode obsoleteNode = node.next;
node.next = node.next.next;
obsoleteNode = null;
}
else
node = node.next;
}
return head;
}
}

Submission Detail

  • 165 / 165 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Remove Duplicates from Sorted List.
  • Memory Usage: 38.4 MB, less than 10.24% of Java online submissions for Remove Duplicates from Sorted List.