15 Jun 2024C++ / Python / TypeScriptEasy

Remove Duplicates from Sorted List

Collected C++, Python, TypeScript solutions for remove duplicates from sorted list. Add a dedicated write-up later if you want deeper notes.

auto-generated entry for Remove Duplicates from Sorted List. the solution files are available below.

solution files

  • C++ remove-duplicates-from-sorted-list/synced-solution.cpp
  • Python remove-duplicates-from-sorted-list/synced-solution.py
  • TypeScript remove-duplicates-from-sorted-list/synced-solution.ts

Solution files

Pythonremove-duplicates-from-sorted-list/synced-solution.py
class Solution:
    def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
        cur = head
        while cur and cur.next:
            if cur.val == cur.next.val:
                cur.next = cur.next.next
            else:
                cur = cur.next
        return head
C++remove-duplicates-from-sorted-list/synced-solution.cpp
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* cur = head;
        while (cur && cur->next) {
            if (cur->val == cur->next->val) cur->next = cur->next->next;
            else cur = cur->next;
        }
        return head;
    }
};
TypeScriptremove-duplicates-from-sorted-list/synced-solution.ts
function deleteDuplicates(head: ListNode | null): ListNode | null {
    let cur = head;
    while (cur && cur.next) {
        if (cur.val === cur.next.val) cur.next = cur.next.next;
        else cur = cur.next;
    }
    return head;
}