Remove Duplicates from Sorted List
Thoughts
Code
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode node = head;
if (head == null || head.next == null) {
return head;
}
while (node != null && node.next != null) {
if (node.next.val == node.val) {
ListNode next = node.next.next;
node.next = next;
} else {
node = node.next;
}
}
return head;
}
}Analysis
Last updated