Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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)