MrainW's Home

All things come to those who wait!

0%

LeetCode 83. Remove Duplicates from Sorted List

Question

Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

  • Solution1
1
2
3
4
5
6
7
8
9
10
11
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode cur = head;
while (cur != null){
while(cur.next != null && cur.val == cur.next.val)
cur.next = cur.next.next;
cur = cur.next;
}
return head;
}
}

Complexity:

Time complexity: O(n)

Space complexity: O(1)

Welcome to my other publishing channels