Пример #1
0
 def insert(self, index, data):
     """
     Purpose: Insert new node at given index position
     :param index: node index to insert
     :param data: new node data
     :return:
     """
     if index == 0 or self.head is None:
         self.push_front(data=data)
         return
     if index >= self.__len__():
         self.push_back(data=data)
         return
     n = Node(data=data)
     i = 0
     current = self.head
     prev = self.head
     while current:
         if index == i:
             prev.next = n
             n.next = current
             self.size += 1
             return
         i += 1
         prev = current
         current = current.next
Пример #2
0
 def push(self, new_data):
     new_node = Node(data = new_data)
     new_node.prev = None # at the beginning
     new_node.next = self.head
     if self.head is not None:
         self.head.prev = new_node
     self.head = new_node
Пример #3
0
def add_two_numbers(l1, l2):
    result_nodes = []
    remember_val = 0

    current_node_l1, current_node_l2 = l1, l2

    while True:
        result_val = (current_node_l1.value if current_node_l1 else 0) + (current_node_l2.value if current_node_l2 else 0) + remember_val

        if result_val > 9:
            result_val -= 10
            remember_val = 1
        else:
            remember_val = 0

        result_nodes.append(Node(result_val))

        current_node_l1, current_node_l2 = current_node_l1.next_node if current_node_l1 else None, current_node_l2.next_node if current_node_l2 else None
        if not current_node_l1 and not current_node_l2:
            break

    if remember_val:
        result_nodes.append(Node(remember_val))

    for i in range(len(result_nodes) - 1):
        result_nodes[i].next_node = result_nodes[i+1]

    return result_nodes[0]
Пример #4
0
 def insert_after(self, prev_node, new_data):
     if prev_node is None:
         print("Error: given prev_node is None")
         return
     new_node = Node(data = new_data)
     new_node.prev = prev_node
     new_node.next = prev_node.next
     if new_node.next is not None: # if new_node.next (prev_node.next) is None it means we are at the end of the list
         new_node.next.prev = new_node    
     prev_node.next = new_node
    def add_to_head(self, item):
        new_node = Node()
        new_node.item = item

        if not self.head:
            self.head = new_node
        else:
            old_head = self.head
            new_node.next = old_head
            self.head = new_node
Пример #6
0
 def insert_before(self, next_node, new_data):
     if next_node is None:
         print("Error: given next_node is None")
         return
     new_node = Node(data = new_data)
     new_node.next = next_node
     new_node.prev = next_node.prev
     if new_node.prev is not None: # if new_node.prev (next_node.prev) is None it means we are at the beginning of the list
         new_node.prev.next = new_node
     next_node.prev = new_node
    def add_to_head(self, item):
        new_node = Node()
        new_node.item = item

        if not self.head:
            self.head = new_node
        else:
            old_head = self.head
            new_node.next = old_head
            self.head = new_node
Пример #8
0
 def append(self, new_data):
     new_node = Node(data = new_data)
     new_node.next = None # at the last
     last = self.tail
     # if list is empty then this is the first node
     if last is None:
         new_node.prev = None
         self.head = new_node
         return
     last.next = new_node
     new_node.prev = last
    def add_to_tail(self, item):
        if not self.head:
            return self.add_to_head(item)

        current = self.head
        old_tail = current

        while (current):
            old_tail = current
            current = current.next

        new_tail = Node()
        new_tail.item = item
        old_tail.next = new_tail
Пример #10
0
    def add_to_tail(self, item):
        if not self.head:
            return self.add_to_head(item)

        current = self.head
        old_tail = current

        while(current):
            old_tail = current
            current = current.next

        new_tail = Node()
        new_tail.item = item
        old_tail.next = new_tail
Пример #11
0
 def push_front(self, data):
     """
     Purpose: Push (add) node to front of SLL
     :param data: new node data
     :return:
     """
     n = Node(data=data)
     if self.head is None:
         self.head = n
         self.size += 1
         return
     old_head = self.head
     self.head = n
     n.next = old_head
     self.size += 1
    def flatten(self, head):
        """
        :type head: Node
        :rtype: Node
        """
        if head is None:
            return None
        dummy = Node(0, None, head, None)
        cur = dummy
        stack = []
        stack.append(head)
        while len(stack) > 0:
            node = stack.pop()

            cur.next = node
            node.prev = cur
            cur = node
            if node.next:
                stack.append(node.next)
            if node.child:
                stack.append(node.child)
                node.child = None
        head = dummy.next
        head.prev = None
        return head
Пример #13
0
    def solve(self, node: Node):

        if node.next:
            copied_node = node.next
            node.val, node.next = copied_node.val, copied_node.next
            copied_node.next = None
        else:
            node = None
Пример #14
0
 def __init__(self):
     super().__init__()
     self.test_cases = [({
         'head':
         Node(
             1,
             Node(5, Node(3, Node(8, Node(10, Node(11, Node(15,
                                                            Node(18)))))))),
         'k':
         1
     }, 15)]
Пример #15
0
 def insert_at_position(self, position, data):
     """
     Insert at given position of the Linked List.
     """
     new_node = Node(data)
     if position == 1:
         # Insert at 1st position,
         # when linked list is empty or if to insert at position 1 when linked list contains items.
         new_node.set_next(self.head)
         self.head = new_node
         self.size += 1
     else:
         # Insert ay any position between 2 to last+1
         temp = self.head
         for i in range(1, position - 1):
             # Traverse still position-1
             temp = temp.get_next()
         new_node.set_next(temp.get_next())
         temp.set_next(new_node)
         self.size += 1
Пример #16
0
    def append(self, value):
        '''
        Appends a new element to the Linked List at the end. If
        the list is empty, then it´s added as both, head and tail nodes.
        Arguments:
        value -- an object to add.
        '''

        new_node = Node()
        new_node.data = value

        if(self.head is None):

            self.head = new_node
            self.tail = new_node
            self.size = 1
            return

        self.size = 1
        self.tail.next = new_node
        new_node.previous = self.tail
        self.tail = new_node
Пример #17
0
    def insert_at_head(self, data):
        """
        Given the object, data
        Then increment the linked list size by one
        And create a new node using the object, data

        When the head is not currently assigned (self.head == None)
        Then assign the newly created node to the head node
        When the head does have a valid object reference
        Then link the newly created node as the head (new_node = self.head)

        Performance: O(1)

        :param data: the object that the node will hold
        """
        self.size = self.size + 1
        new_node = Node(data)

        if self.head is not None:
            # link the new node to the current head
            new_node.next_node = self.head

        # assign the head position to the new node
        self.head = new_node
Пример #18
0
    def solve(self, head: Node, node: Node):

        new_head = node
        new_tail = node
        current = head

        while current is not None:
            current_copy = Node(current.val)
            if current < node:
                new_head, new_head.next = current_copy, new_head
            else:
                new_tail.next = current_copy
                new_tail = current_copy

            current = current.next

        return new_head
    def test_something(self):
        node1 = Node(1)
        node2 = Node(2)
        node3 = Node(3)
        node4 = Node(4)
        node5 = Node(5)

        node1.next_node = node2
        # node2.next_node = node3
        # node3.next_node = node4
        # node4.next_node = node5

        remove_nth_from_end(node1, 2)

        self.assertEqual(True, False)
Пример #20
0
 def push_back(self, data):
     """
     Purpose: Push (add) node to back of SLL
     :param data: new node data
     :return:
     """
     if self.head is None:
         self.push_front(data=data)
         return
     n = Node(data=data)
     current = self.head
     while current:
         if current.next is None:
             current.next = n
             self.size += 1
             return
         current = current.next
Пример #21
0
    def insert_at_tail(self, data):
        """
        Given the object, data
        Then increment the linked list size by one
        And create a new node using the object, data
        And set the current node reference to the head node

        Given that the current node is not None
        Then assign the current node to the current node's next node
        When current_node is None
        Then assign the current node to the new node (effectively inserting at the list's tail)

        :param data: the data value to insert into the linked list
        """
        self.size = self.size + 1
        new_node = Node(data)
        current_node = self.head

        while current_node.next_node is not None:
            current_node = current_node.next_node

        current_node.next_node = new_node
Пример #22
0
    def insert_at(self, index, value):
        '''
        Inserts a value at the given position.
        index -- the position as an int.
        value -- an object.
        '''
        if not isinstance(index, int):
            raise TypeError("index is not of type int")
        if index < 0 or index > self.size:
            raise LookupError("index out of bounds")

        new_node = Node()
        new_node.data = value

        if self.head is None:

            self.head = new_node
            self.tail = new_node
            self.size = 1
            return

        if index == 0:

            new_node.next = self.head
            self.head.previous = new_node
            self.head = new_node
            self.size = 1

        else:

            current_node = self.head
            for i in range(index-1):
                current_node = current_node.next
            new_node.previous = current_node
            new_node.next = current_node.next
            current_node.next = new_node
            if(new_node.next is None):
                self.tail = new_node
            self.size = 1
Пример #23
0
 def __init__(self):
     super().__init__()
     self.test_cases = [({
         'head': None
     }, None), ({
         'head': Node(1)
     }, Node(1)), ({
         'head': Node(1, _next=Node(2))
     }, Node(1, _next=Node(2))),
                        ({
                            'head':
                            Node(1,
                                 _next=Node(1, _next=Node(1,
                                                          _next=Node(2))))
                        }, Node(1, _next=Node(2)))]
Пример #24
0
    def test_something(self):
        l1_node1 = Node(2)
        l1_node2 = Node(4)
        l1_node3 = Node(3)

        l1_node1.next_node = l1_node2
        l1_node2.next_node = l1_node3

        l2_node1 = Node(5)
        l2_node2 = Node(6)
        l2_node3 = Node(4)

        l2_node1.next_node = l2_node2
        l2_node2.next_node = l2_node3

        l3_node1 = Node(7)
        l3_node2 = Node(0)
        l3_node3 = Node(8)

        l3_node1.next_node = l3_node2
        l3_node2.next_node = l3_node3

        result = add_two_numbers(l1_node1, l2_node1)

        self.assertEqual(result.value, l3_node1.value)
        self.assertEqual(result.next_node.value, l3_node2.value)
        self.assertEqual(result.next_node.next_node.value, l3_node3.value)
    # def append(self, cur, dummy):
    #     while cur:
    #         dummy.next = cur
    #         cur.prev = dummy
    #         dummy = dummy.next
    #         if cur.child:
    #             next = cur.next
    #             dummy = self.append(cur.child, dummy)
    #             cur.child = None
    #             cur = next
    #         else:
    #             cur = cur.next
    #
    #     return dummy
#
n1 = Node(1, None, None, None)
n2 = Node(2, n1, None, None)
n3 = Node(3, n2, None, None)
n4 = Node(4, n3, None, None)
n5 = Node(5, n4, None, None)
n6 = Node(6, n5, None, None)
n7 = Node(7, n6, None, None)
n8 = Node(8, n7, None, None)
n9 = Node(9, n8, None, None)
n10 = Node(10, n9, None, None)
n11 = Node(11, n10, None, None)
n12 = Node(12, n11, None, None)
n1.next = n2
n2.next = n3
n3.next = n4
n4.next = n5