def to_linked_list(values: list) -> ListNode: if values: head = ListNode(values[0]) current_node = head for i in values[1:]: current_node.next = ListNode(i) current_node = current_node.next return head else: return None
def remove_dupes(head: ListNode) -> ListNode: if head: node = ListNode(head.val) head = head.next else: return ListNode("") new_head = node while head: if head.val != node.val: node.next = ListNode(head.val) node = node.next head = head.next return new_head
def remove_linked_list_val(head: ListNode, val: int) -> ListNode: if head is None: return head start = head previous = None while start: if start.val == val: if previous is None: # Start of list head = head.next start = head else: if not start.next: # End of list previous.next = None break else: start = start.next previous.next = start else: previous = start start = start.next print(previous.val, start.val) if not head: return ListNode(None) return head
def merge_sorted_lists(l1: ListNode, l2: ListNode) -> ListNode: def to_list(head: ListNode) -> list: output = list() while head: output.append(head.val) head = head.next return output def to_linked_list(values: list) -> ListNode: if values: head = ListNode(values[0]) current_node = head for i in values[1:]: current_node.next = ListNode(i) current_node = current_node.next return head else: return None linked_list_values = list() for i in to_list(l1) + to_list(l2): if i is not None: linked_list_values.append(i) if linked_list_values != []: linked_list_values.sort() return to_linked_list(linked_list_values) else: return ListNode('')
def remove_linked_list_val_messy(head: ListNode, val: int) -> ListNode: if not head.next: # Len = 1 or empty list edge case if head.val != val: return head else: return ListNode(None) while head.val == val: # First list node to be removed edge case if head.next: head = head.next else: return ListNode(None) slow = head fast = head.next while fast.next: print(slow.val, fast.val) if fast.val == val: slow.next = slow.next.next slow = slow.next fast = fast.next.next else: slow = slow.next fast = fast.next if not fast.next: if fast.val == val: slow.next = None break if fast.val == val: # Two item list with last val match edge case slow.next = None print(head.to_list()) return head
def remove_nth_node_from_end(head: ListNode, n: int) -> ListNode: slow = head fast = head for _ in range(n): fast = fast.next if not fast: # n = first item edge case head = slow.next if not head: # list of one item edge case head = ListNode("") return head while fast.next is not None: fast = fast.next slow = slow.next slow.next = slow.next.next return head
def test_3204_true(self): input = has_cycle(ListNode.build_circular([3, 2, 0, -4])) self.assertTrue(input)
def test_blank(self): input = remove_dupes(ListNode.build([])) output = ListNode.build([]) self.assertEqual(input, output)
def test_1234567888(self): input = remove_dupes(ListNode.build([1, 2, 3, 4, 5, 6, 7, 8, 8, 8])) output = ListNode.build([1, 2, 3, 4, 5, 6, 7, 8]) self.assertEqual(input, output)
def test_11112233(self): input = remove_dupes(ListNode.build([0, 11, 11, 22, 33])) output = ListNode.build([0, 11, 22, 33]) self.assertEqual(input, output)
def test_1_1(self): # Test works on LeetCode, but not here input = remove_nth_node_from_end(ListNode.build([1]), 1) output = ListNode.build("") self.assertEqual(input, output)
def test_12345678_false(self): input = has_cycle(ListNode.build([1, 2, 3, 4, 5, 6, 7, 8])) self.assertFalse(input)
def test_1263456_6(self): input = remove_linked_list_val(ListNode.build([1, 2, 6, 3, 4, 5, 6]), 6) self.assertEqual(input, ListNode.build([1, 2, 3, 4, 5]))
def del_node(node: ListNode): while node.next.next: node.val = node.next.val node = node.next node.val = node.next.val node.next = None
def test_45_4(self): head = ListNode.build([4, 5]) node = head del_node(node) out_list = ListNode.build([5]) self.assertEqual(head, out_list)
def test_4519_1(self): head = ListNode.build([4, 5, 1, 9]) node = head.next.next del_node(node) out_list = ListNode.build([4, 5, 9]) self.assertEqual(head, out_list)
def test_123456789_9(self): input = remove_nth_node_from_end( ListNode.build([1, 2, 3, 4, 5, 6, 7, 8, 9]), 9) output = ListNode.build([2, 3, 4, 5, 6, 7, 8, 9]) self.assertEqual(input, output)
def test_123_3(self): input = remove_nth_node_from_end(ListNode.build([1, 2, 3]), 3) output = ListNode.build([2, 3]) self.assertEqual(input, output)
def test_12_true(self): input = has_cycle(ListNode.build_circular([1, 2])) self.assertTrue(input)
def test_81632_519100(self): list1 = ListNode.build([8, 16, 32]) list2 = ListNode.build([5, 19, 100]) input = merge_sorted_lists(list1, list2) output = ListNode.build([5, 8, 16, 19, 32, 100]) self.assertEqual(input, output)
def test_blank_false(self): input = has_cycle(ListNode.build([])) self.assertFalse(input)
def test_uneven_lists(self): list1 = ListNode.build([1, 2, 3, 4]) list2 = ListNode.build([-99, 4, 32]) input = merge_sorted_lists(list1, list2) output = ListNode.build([-99, 1, 2, 3, 4, 4, 32]) self.assertEqual(input, output)
def test_12345(self): head = ListNode.build([1, 2, 3, 4, 5]) reversed = ListNode.build([5, 4, 3, 2, 1]) self.assertEqual(reverse_ll(head), reversed)
def test_12_2(self): input = remove_nth_node_from_end(ListNode.build([1, 2]), 2) output = ListNode.build([2]) self.assertEqual(input, output)
def test_124_134(self): list1 = ListNode.build([1, 2, 4]) list2 = ListNode.build([1, 3, 4]) input = merge_sorted_lists(list1, list2) output = ListNode.build([1, 1, 2, 3, 4, 4]) self.assertEqual(input, output)
def test_blank(self): head = ListNode.build([]) reversed = ListNode.build([]) self.assertEqual(reverse_ll(head), reversed)
def test_minus1248_minus6945(self): list1 = ListNode.build([-12, 4, 8]) list2 = ListNode.build([-6, 9, 45]) input = merge_sorted_lists(list1, list2) output = ListNode.build([-12, -6, 4, 8, 9, 45]) self.assertEqual(input, output)
def test_112(self): input = remove_dupes(ListNode.build([1, 1, 2])) output = ListNode.build([1, 2]) self.assertEqual(input, output)
def test_blank_lists(self): list1 = ListNode.build([]) list2 = ListNode.build([]) input = merge_sorted_lists(list1, list2) output = ListNode('') self.assertEqual(input, output)
def test_short_true(self): head = ListNode.build([1, 2, 2, 1]) self.assertTrue(is_palindrome(head))