Skip to content

Commit d4b7dd4

Browse files
authored
Merge pull request #1530 from ivanpenaloza/january28
adding algo
2 parents d09ee01 + b3cafe0 commit d4b7dd4

3 files changed

Lines changed: 120 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
# Definition for singly-linked list.
5+
class ListNode:
6+
def __init__(self, x):
7+
self.val = x
8+
self.next = None
9+
10+
class Solution:
11+
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
12+
# Create a dummy node to handle edge cases where head is removed
13+
dummy = ListNode(0)
14+
dummy.next = head
15+
prev = dummy
16+
17+
while head:
18+
# Check if current node has duplicates
19+
if head.next and head.val == head.next.val:
20+
# Skip all nodes with the same value
21+
while head.next and head.val == head.next.val:
22+
head = head.next
23+
# Link prev to the node after duplicates
24+
prev.next = head.next
25+
else:
26+
# No duplicate, move prev forward
27+
prev = prev.next
28+
29+
# Move to next node
30+
head = head.next
31+
32+
return dummy.next
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ListNode } from './ListNode';
2+
3+
function deleteDuplicates(head: ListNode | null): ListNode | null {
4+
// Create a dummy node to handle edge cases where head is removed
5+
const dummy = new ListNode(0);
6+
dummy.next = head;
7+
let prev = dummy;
8+
9+
while (head) {
10+
// Check if current node has duplicates
11+
if (head.next && head.val === head.next.val) {
12+
// Skip all nodes with the same value
13+
while (head.next && head.val === head.next.val) {
14+
head = head.next;
15+
}
16+
// Link prev to the node after duplicates
17+
prev.next = head.next;
18+
} else {
19+
// No duplicate, move prev forward
20+
prev = prev.next!;
21+
}
22+
23+
// Move to next node
24+
head = head.next;
25+
}
26+
27+
return dummy.next;
28+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_62_remove_duplicates_from_sorted_linked_list_ii import Solution, ListNode
4+
5+
class RemoveDuplicatesFromSortedLinkedListsIITestCase(unittest.TestCase):
6+
7+
def create_linked_list(self, values):
8+
"""
9+
Helper function to create a linked list from a list of values.
10+
11+
:param values: List of node values
12+
:return: Head of the linked list
13+
"""
14+
if not values:
15+
return None
16+
17+
head = ListNode(values[0])
18+
current = head
19+
20+
for val in values[1:]:
21+
current.next = ListNode(val)
22+
current = current.next
23+
24+
return head
25+
26+
def linked_list_to_list(self, head):
27+
"""
28+
Helper function to convert linked list to Python list.
29+
30+
:param head: Head of the linked list
31+
:return: List of values
32+
"""
33+
result = []
34+
current = head
35+
36+
while current:
37+
result.append(current.val)
38+
current = current.next
39+
40+
return result
41+
42+
def test_first_pattern(self):
43+
# Example 1: Input: head = [1,2,3,3,4,4,5]
44+
# Output: [1,2,5]
45+
solution = Solution()
46+
head = self.create_linked_list([1, 2, 3, 3, 4, 4, 5])
47+
output = solution.deleteDuplicates(head)
48+
result = self.linked_list_to_list(output)
49+
target = [1, 2, 5]
50+
self.assertEqual(result, target)
51+
52+
def test_second_pattern(self):
53+
# Example 2: Input: head = [1,1,1,2,3]
54+
# Output: [2,3]
55+
solution = Solution()
56+
head = self.create_linked_list([1, 1, 1, 2, 3])
57+
output = solution.deleteDuplicates(head)
58+
result = self.linked_list_to_list(output)
59+
target = [2, 3]
60+
self.assertEqual(result, target)

0 commit comments

Comments
 (0)