diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_62_remove_duplicates_from_sorted_linked_list_ii.py b/src/my_project/interviews/top_150_questions_round_22/ex_62_remove_duplicates_from_sorted_linked_list_ii.py new file mode 100644 index 00000000..bfa08924 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_62_remove_duplicates_from_sorted_linked_list_ii.py @@ -0,0 +1,32 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +# Definition for singly-linked list. +class ListNode: + def __init__(self, x): + self.val = x + self.next = None + +class Solution: + def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]: + # Create a dummy node to handle edge cases where head is removed + dummy = ListNode(0) + dummy.next = head + prev = dummy + + while head: + # Check if current node has duplicates + if head.next and head.val == head.next.val: + # Skip all nodes with the same value + while head.next and head.val == head.next.val: + head = head.next + # Link prev to the node after duplicates + prev.next = head.next + else: + # No duplicate, move prev forward + prev = prev.next + + # Move to next node + head = head.next + + return dummy.next \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_62_remove_duplicates_from_sorted_linked_list_ii.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_62_remove_duplicates_from_sorted_linked_list_ii.ts new file mode 100644 index 00000000..ec3aacdf --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_62_remove_duplicates_from_sorted_linked_list_ii.ts @@ -0,0 +1,28 @@ +import { ListNode } from './ListNode'; + +function deleteDuplicates(head: ListNode | null): ListNode | null { + // Create a dummy node to handle edge cases where head is removed + const dummy = new ListNode(0); + dummy.next = head; + let prev = dummy; + + while (head) { + // Check if current node has duplicates + if (head.next && head.val === head.next.val) { + // Skip all nodes with the same value + while (head.next && head.val === head.next.val) { + head = head.next; + } + // Link prev to the node after duplicates + prev.next = head.next; + } else { + // No duplicate, move prev forward + prev = prev.next!; + } + + // Move to next node + head = head.next; + } + + return dummy.next; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_62_remove_duplicates_from_sorted_linked_list_ii_round_22.py b/tests/test_150_questions_round_22/test_62_remove_duplicates_from_sorted_linked_list_ii_round_22.py new file mode 100644 index 00000000..73e54205 --- /dev/null +++ b/tests/test_150_questions_round_22/test_62_remove_duplicates_from_sorted_linked_list_ii_round_22.py @@ -0,0 +1,60 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_62_remove_duplicates_from_sorted_linked_list_ii import Solution, ListNode + +class RemoveDuplicatesFromSortedLinkedListsIITestCase(unittest.TestCase): + + def create_linked_list(self, values): + """ + Helper function to create a linked list from a list of values. + + :param values: List of node values + :return: Head of the linked list + """ + if not values: + return None + + head = ListNode(values[0]) + current = head + + for val in values[1:]: + current.next = ListNode(val) + current = current.next + + return head + + def linked_list_to_list(self, head): + """ + Helper function to convert linked list to Python list. + + :param head: Head of the linked list + :return: List of values + """ + result = [] + current = head + + while current: + result.append(current.val) + current = current.next + + return result + + def test_first_pattern(self): + # Example 1: Input: head = [1,2,3,3,4,4,5] + # Output: [1,2,5] + solution = Solution() + head = self.create_linked_list([1, 2, 3, 3, 4, 4, 5]) + output = solution.deleteDuplicates(head) + result = self.linked_list_to_list(output) + target = [1, 2, 5] + self.assertEqual(result, target) + + def test_second_pattern(self): + # Example 2: Input: head = [1,1,1,2,3] + # Output: [2,3] + solution = Solution() + head = self.create_linked_list([1, 1, 1, 2, 3]) + output = solution.deleteDuplicates(head) + result = self.linked_list_to_list(output) + target = [2, 3] + self.assertEqual(result, target) \ No newline at end of file