def linked_list_add(first_linked_list, second_linked_list, carry):
    """
    The numbers are stored in reverse order
    """
    if not first_linked_list and not second_linked_list and carry == 0:
        return None

    value = carry
    if first_linked_list:
        value = value + first_linked_list.data

    if second_linked_list:
        value = value + second_linked_list.data

    result = value % 10  # the last digit
    result_linked_list = LinkedListNode(result)

    if first_linked_list or second_linked_list:
        next_node = linked_list_add(None if not first_linked_list else first_linked_list.next,
                                    None if not second_linked_list else second_linked_list.next,
                                    0 if value < 10 else 1)

        result_linked_list.next = next_node

    return result_linked_list
def linked_list_add_forward(linked_list_one, linked_list_two, carry):
    # convert the first list to an integer
    current_node = linked_list_one
    first_integer_value = 0
    while current_node:
        first_integer_value = first_integer_value * 10 + current_node.data
        current_node = current_node.next

    current_node = linked_list_two
    second_integer_value = 0
    while current_node:
        second_integer_value = second_integer_value * 10 + current_node.data
        current_node = current_node.next

    result = first_integer_value + second_integer_value

    # convert result back to a linked list
    divided_by_ten = result
    next_node = None
    while divided_by_ten != 0:
        current_digit = divided_by_ten % 10
        divided_by_ten = divided_by_ten / 10
        new_node = LinkedListNode(current_digit)
        new_node.next = next_node
        next_node = new_node

    return new_node
def linked_list_add(first_linked_list, second_linked_list, carry):
    """
    The numbers are stored in reverse order
    """
    if not first_linked_list and not second_linked_list and carry == 0:
        return None

    value = carry
    if first_linked_list:
        value = value + first_linked_list.data

    if second_linked_list:
        value = value + second_linked_list.data

    result = value % 10 # the last digit
    result_linked_list = LinkedListNode(result)

    if first_linked_list or second_linked_list:
        next_node = linked_list_add(None if not first_linked_list else first_linked_list.next,
                                    None if not second_linked_list else second_linked_list.next,
                                    0 if value < 10 else 1)

        result_linked_list.next = next_node

    return result_linked_list
def create_circular_list(length_before_loop, loop_length):
    root = LinkedListNode(random.randint(0, 9))
    previous_node = root

    # create the section before the loop
    for i in range(0, length_before_loop):
        new_node = LinkedListNode(random.randint(0, 10))
        previous_node.next = new_node
        previous_node = new_node

    # this is the node the loop points to
    loop_back_node = previous_node
    print("The beginning of the loop is: " + str(loop_back_node))

    # create the nodes in the loop
    for i in range(0, loop_length):
        new_node = LinkedListNode(random.randint(0, 10))
        previous_node.next = new_node
        previous_node = new_node

    # create the cycle - point the end of the linked list back to the
    # "loop back node"
    previous_node.next = loop_back_node

    return root
def linked_list_add_forward(linked_list_one, linked_list_two, carry):
    # convert the first list to an integer
    current_node = linked_list_one
    first_integer_value = 0
    while current_node:
        first_integer_value = first_integer_value * 10 + current_node.data
        current_node = current_node.next

    current_node = linked_list_two
    second_integer_value = 0
    while current_node:
        second_integer_value = second_integer_value * 10 + current_node.data
        current_node = current_node.next

    result = first_integer_value + second_integer_value

    # convert result back to a linked list
    divided_by_ten = result
    next_node = None
    while divided_by_ten != 0:
        current_digit = divided_by_ten % 10
        divided_by_ten = divided_by_ten / 10
        new_node = LinkedListNode(current_digit)
        new_node.next = next_node
        next_node = new_node

    return new_node
 def push(self, value):
     """Method for adding a node to the list"""
     node = LinkedListNode(value)
     if self.is_empty():
         self.head = node
     else:
         node.next = self.head
         self.head = node
Beispiel #7
0
 def test_get_tail(self):
     test_list = LinkedList()
     test_node_1 = LinkedListNode(1)
     test_node_2 = LinkedListNode(2)
     test_node_3 = LinkedListNode(3)
     test_list.append(test_node_1)
     test_list.append(test_node_2)
     test_list.append(test_node_3)
     self.assertEqual(test_list.get_tail().value, 3)
def create_random_linked_list(number_of_nodes):
    root = LinkedListNode(random.randint(0, 9))
    current_node = root
    for i in range(0, number_of_nodes - 1):
        random_key = random.randint(0, 9)
        new_node = LinkedListNode(random_key)
        current_node.next = new_node
        current_node = new_node

    return root
Beispiel #9
0
 def test_multiple_append(self):
     test_list = LinkedList()
     test_node_1 = LinkedListNode(1)
     test_node_2 = LinkedListNode(2)
     test_node_3 = LinkedListNode(3)
     test_list.append(test_node_1)
     test_list.append(test_node_2)
     test_list.append(test_node_3)
     self.assertEqual(test_list.head.value, 1)
     self.assertEqual(test_list.head.next.value, 2)
     self.assertEqual(test_list.head.next.next.value, 3)
Beispiel #10
0
def linked_list_sort(l: LinkedList) -> LinkedList:
    if not l.head:
        return l

    pivot_node = l.head
    pivot = pivot_node.value
    less_head = less_tail = LinkedListNode(0)
    greater_head = greater_tail = LinkedListNode(0)

    curr = l.head.next
    while curr:
        if curr.value < pivot:
            less_tail.next = curr
            less_tail = less_tail.next
        else:
            greater_tail.next = curr
            greater_tail = greater_tail.next

        curr = curr.next

    less_list = LinkedList()
    less_list.head = less_head.next
    less_tail.next = None
    less_list = linked_list_sort(less_list)

    greater_list = LinkedList()
    greater_list.head = greater_head.next
    greater_tail.next = None
    greater_list = linked_list_sort(greater_list)

    dummy_head = tail = LinkedListNode(0)
    if less_list:
        tail.next = less_list.head
        tail = less_list.tail
    tail.next = pivot_node
    tail = tail.next
    if greater_list:
        tail.next = greater_list.head
        tail = greater_list.tail

    l.head = dummy_head.next
    l.tail = tail
    tail.next = None
    # print(l)
    return l
Beispiel #11
0
 def enqueue(self, value):
     """Method for adding a node to the end of the queue"""
     node = LinkedListNode(value)
     if self.is_empty():
         self.head = node
         self.tail = node
     else:
         self.tail.next = node
         self.tail = node
Beispiel #12
0
def reversed(ll):
    new_head = None
    curr = ll.head
    while curr:
        new_head = LinkedListNode(curr.val, new_head)
        curr = curr.next

    reversed_ll = LinkedList()
    reversed_ll.head = new_head
    return reversed_ll
Beispiel #13
0
    def test_append(self):
        """Can append number"""
        test_list = LinkedList()
        test_list.append(4)
        self.assertEqual(test_list.head.value, 4)

        """Can append node"""
        test_list = LinkedList()
        test_node = LinkedListNode(4)
        test_list.append(test_node)
        self.assertEqual(test_list.head.value, 4)
Beispiel #14
0
def build_palindrome_list():
    root = LinkedListNode(5)
    previous_node = root
    for i in range(0, 2):
        new_node = LinkedListNode(random.randint(0, 9))
        previous_node.next = new_node
        previous_node = new_node

    stack = []
    current_node = root
    while current_node.next != None:  # all but the last one
        stack.append(current_node.data)
        current_node = current_node.next

    while len(stack) != 0:
        data = stack.pop()
        new_node = LinkedListNode(data)
        previous_node.next = new_node
        previous_node = new_node

    return root
Beispiel #15
0
def create_list(tree):
    queue = [tree.root]
    level = 0
    list_of_lists = []
    while queue:
        head = LinkedListNode(queue[0].value)
        current_node = head
        children = []
        if queue[0].left: children.append(queue[0].left)
        if queue[0].right: children.append(queue[0].right)
        for node in queue[1:]:
            current_node.next = LinkedListNode(node.value)
            current_node = current_node.next
            if node.left: 
                children.append(node.left)
            if node.right:
                children.append(node.right)
        list_of_lists.append(head)
        queue = children
        
    return list_of_lists
Beispiel #16
0
 def test_delete_next(self):
     test_node_1 = LinkedListNode(1)
     test_node_1.append(2)
     test_node_1.append(3)
     self.assertEqual(test_node_1.next.value, 2)
     test_node_1.delete_next()
     self.assertEqual(test_node_1.next.value, 3)
Beispiel #17
0
def sum_lists_forward(list1, list2):
    """
    Returns sum of list1 and list2 as if the ones digit were in the first node.
    [7, 1, 6] + [5, 9, 2] -> [2, 1, 9]
    """
    overflow = 0
    node1 = list1.head
    node2 = list2.head
    new_list = LinkedList()
    new_tail = None
    while node1 or node2 or overflow:
        node1_val = node1.val if node1 else 0
        node2_val = node2.val if node2 else 0
        overflow, digit_sum = divmod(node1_val + node2_val + overflow, 10)
        if new_list.head:
            new_tail.next = LinkedListNode(digit_sum)
            new_tail = new_tail.next
        else:
            new_list.head = LinkedListNode(digit_sum)
            new_tail = new_list.head
        node1 = node1.next if node1 else None
        node2 = node2.next if node2 else None
    return new_list
def get_sorted_line_seg_endpoints(trajectory_line_segments):
    line_segment_endpoints = []
    cur_id = 0

    for traj_segment in trajectory_line_segments:
        list_node = LinkedListNode(traj_segment)
        line_segment_endpoints.append(TrajectoryLineSegmentEndpoint(traj_segment.line_segment.start.x, \
                                                                    traj_segment, cur_id, list_node))
        line_segment_endpoints.append(TrajectoryLineSegmentEndpoint(traj_segment.line_segment.end.x, \
                                                                    traj_segment, cur_id, list_node))
        cur_id += 1

    return sorted(line_segment_endpoints,
                  key=attrgetter('horizontal_position'))
Beispiel #19
0
def get_sorted_line_seg_endpoints(tls_list):
    line_segment_endpoints = []
    cur_id = 0
    for tls in tls_list:
        list_node = LinkedListNode(tls)
        e1 = TrajectoryLineSegmentEndpoint(tls.rsegment.start.x, tls, cur_id,
                                           list_node)
        e2 = TrajectoryLineSegmentEndpoint(tls.rsegment.end.x, tls, cur_id,
                                           list_node)
        line_segment_endpoints.append(e1)
        line_segment_endpoints.append(e2)
        cur_id += 1
    return sorted(line_segment_endpoints,
                  key=attrgetter('horizontal_position'))
Beispiel #20
0
def multiple_linked_list(x, y, result=None, carry_over=0):
    if not x and not y:
        return result

    x_vlaue = x.value if x else 0
    y_value = y.value if y else 0

    tens, ones = divmod(x_vlaue + y_value + carry_over, 10)

    if not result:
        result = LinkedList(ones)
    else:
        result.append_node_to_tail(LinkedListNode(ones))

    return multiple_linked_list(x.next if x else None,
                                y.next if y else None,
                                result=result,
                                carry_over=tens)
def test_str():
    """
  Linked list can be made into string form.
  """

    ll = LinkedListNode(2)
    ll.appendToTail(1)
    ll.appendToTail(2)
    ll.appendToTail(6)
    ll.appendToTail(6)

    expected = '[2] -> [1] -> [2] -> [6] -> [6]'
    actual = str(ll)

    assert actual == expected
 def test_exceptions(self):
     list, node = self.get_single_node_list()
     list.remove_node(node)
     self.assertRaises(Exception, list.remove_node, node)
     self.assertRaises(Exception, list.remove_node, LinkedListNode(0))
 def get_single_node_list(self):
     list = LinkedList()
     node = LinkedListNode(0)
     list.add_last_node(node)
     return (list, node)
Beispiel #24
0

def reverse(node):
    if node == None:
        return None
    if node.next == None:
        return node
    prev_node = None
    current = node
    while current:
        temp = current.next
        current.next = prev_node
        prev_node = current
        current = temp
    return (prev_node)


def display_all_values(node):
    while True:
        print(node.value)
        if node.next == None:
            break
        node = node.next


a = LinkedListNode(1)
a.next = LinkedListNode(2)
a.next.next = LinkedListNode(3)
a.next.next.next = LinkedListNode(4)
display_all_values(a)
display_all_values(reverse(a))
given only access to that node
"""
from linked_list import LinkedListNode
import random


def delete_middle(middle_node):
    """
    Since you are not given access to the head of the list, you can simply
    copy the data from the next node to the current node and then delete the
    next node.
    This solution does not work if the node to be deleted is the last node.
    """
    middle_node.data = middle_node.next.data
    middle_node.next = middle_node.next.next


root = LinkedListNode(5)
current_node = root
for i in range(0, 10):
    random_key = random.randint(0, 10)
    new_node = LinkedListNode(random_key)
    current_node.next = new_node
    current_node = new_node
    if i == 4:
        middle_node = current_node

print("Original list: " + str(root))
delete_middle(middle_node)
print("List with middle node deleted: " + str(root))
Beispiel #26
0
 def test_init(self):
     test_node = LinkedListNode(4)
     self.assertEqual(test_node.value, 4)
Beispiel #27
0
    def test_it(self):
        node1 = LinkedListNode(1)
        node2 = LinkedListNode(2)
        node3 = LinkedListNode(3)
        node4 = LinkedListNode(4)
        node5 = LinkedListNode(5)

        # List1, node1 -> 2 -> 3
        node1.next = node2
        node2.next = node3

        # List2, node5 -> 4 -> 3
        node5.next = node4
        node4.next = node3

        assert extract_common_node(node1, node5) == [node3]
"""
Write a program that reverses a linked list without using more than O(1) storage
"""
from linked_list import LinkedListNode
import random

head = LinkedListNode(random.randint(0, 20))
previous = head
for i in range(0, 5):
    node = LinkedListNode(random.randint(0, 20))
    previous.next = node
    previous = node

node.next = None

print(head)

current = head
next = current.next
current.next = None
while next:
    old_next = next.next
    next.next = current
    current = next
    next = old_next

print(current)
"""
Write a function to delete a node (except the tail) in a singly linked list,
given only access to that node.

Suppose the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node 
with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

https://leetcode.com/problems/delete-node-in-a-linked-list/
"""

from linked_list import LinkedListNode


# @param head LinkedListNode the head of a linked list
# @param node LinkedListNode the linked list node to be deleted
def delete_node(node):
    if node and node.next:
        node.data = node.next.data
        node.next = node.next.next


if __name__ == '__main__':
    head = LinkedListNode(1)
    head.next = LinkedListNode(2)
    head.next.next = LinkedListNode(3)
    head.next.next.next = LinkedListNode(4)

    delete_node(head.next.next)
    assert str(head) == "1, 2, 4"
    print("All test cases passed.")
Beispiel #30
0
    def test_it3(self):
        node1 = LinkedListNode(1)
        node2 = LinkedListNode(2)
        node3 = LinkedListNode(3)
        node4 = LinkedListNode(4)
        node5 = LinkedListNode(1)

        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5

        assert not is_circulation(node1)
"""
Given a sorted linked list, delete all duplicates such that each element appears 
only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->, return 1->2->3.
"""
from linked_list import LinkedListNode

head = LinkedListNode(1)
head.next = LinkedListNode(1)
head.next.next = LinkedListNode(2)


# @param {LinkedListNode} head head of a linked list
def remove_duplicates(head):
    if not head or not head.next:
        return head

    slow_pointer = head
    fast_pointer = head.next
    while fast_pointer:
        if slow_pointer.data == fast_pointer.data:
            slow_pointer.next = fast_pointer.next
            fast_pointer = fast_pointer.next
        else:
            slow_pointer = slow_pointer.next
            fast_pointer = fast_pointer.next

    return head
Beispiel #32
0
    def test_memorize_nodes(self):
        node1 = LinkedListNode(1)
        node2 = LinkedListNode(2)
        node3 = LinkedListNode(3)
        node4 = LinkedListNode(2)
        node5 = LinkedListNode(1)

        node1.next = node2
        node2.next = node3
        node3.next = node4
        node4.next = node5

        actual = memorize_nodes(node1)
        expect = {1: [node1, node5], 2: [node2, node4], 3: [node3]}
        assert actual == expect