def sum_lists(l1, l2):
    borrow = 0
    head = None
    tail = None

    while l1 or l2:
        sum_lists = borrow + (0 if not l1 else l1.val)
        sum_lists += 0 if not l2 else l2.val

        if sum_lists > 9:
            borrow = 1
            sum_lists -= 10
        else:
            borrow = 0

        new_node = Node(sum_lists)
        if not head:
            head = new_node
        else:
            tail.next = new_node
        tail = new_node

        l1 = None if not l1 else l1.next
        l2 = None if not l2 else l2.next

    if borrow:
        tail.next = Node(borrow)
        tail = tail.next

    return head
Пример #2
0
def FindMergeNode(headA, headB):
    traverse1 = Node()
    traverse2 = Node()
    traverse1 = headA
    traverse2 = headB
    lenA = 0
    lenB = 0
    while traverse1 != None:
        lenA += 1
        traverse1 = traverse1.next
    while traverse2 != None:
        lenB += 1
        traverse2 = traverse2.next
    traverse1 = headA
    traverse2 = headB
    if lenA > lenB:
        extra = lenA - lenB
        while extra:
            extra -= 1
            traverse1 = traverse.next
    else:
        if lenB > lenA:
            extra = lenB - lenA
            while extra:
                extra -= 1
                traverse2 = traverse2.next

    while traverse1 != traverse2:
        traverse1 = traverse1.next
        traverse2 = traverse2.next

    return traverse1.data
Пример #3
0
    def sort(self, values):
        n = len(values) * 2
        m = self.__get_prime_number(n)
        bucket = [None] * n

        # insert into value list
        for value in values:
            key = value % m

            new_node = Node(value)
            if bucket[key] is None:
                bucket[key] = new_node
            else:
                node = bucket[key]
                while node.succ_node is not None:
                    node = node.succ_node

                node.succ_node = new_node
                new_node.pred_node = node

        # get sorted values
        sorted_values = []
        for item in bucket:
            if item is None:
                continue

            node = item
            while node is not None:
                sorted_values.append(node.data)
                node = node.succ_node

        return sorted_values
Пример #4
0
 def init_linked_list(self):
     node_3 = Node(KeyValue(3, "val3"))
     self.linked_list.add_node(node_3)
     node_2 = Node(KeyValue(2, "val2"))
     self.linked_list.add_node(node_2)
     node_1 = Node(KeyValue(1, "val1"))
     self.linked_list.add_node(node_1)
Пример #5
0
def get_linked_list_from_stack(stack) -> LinkedList:
    ll = LinkedList()
    ll.set_head(Node(stack.pop()))

    while len(stack) > 0:
        ll.append_to_tail(Node(stack.pop()))
    return ll
Пример #6
0
 def add(self, val):
     # if val is a node, pass in
     if type(val) == type(Node()):
         self.add_helper(val)
     # otherwise cast to node
     else:
         self.add(Node(val))
Пример #7
0
def test_insert_after_node():
    linked_list = LinkedList()
    n1 = Node(1)
    n2 = Node(2)
    n3 = Node(3)
    n4 = Node(4)
    n5 = Node(5)

    # inserting into empty list
    linked_list.insert(None, n1)
    assert linked_list.head == n1
    assert linked_list.tail == n1

    linked_list.add_in_tail(n2)
    linked_list.add_in_tail(n3)

    # inserting into list middle
    linked_list.insert(n1, n4)

    assert n1.next == n4
    assert n4.next == n2

    # inserting into tail:
    linked_list.insert(n3, n5)

    assert n3.next == n5
    assert n5.next is None
    assert linked_list.tail == n5
Пример #8
0
 def test_partition(self):
     testCases = [([5, 8, 9, 4, 2, 1, 3, 4, 5, 7, 8, 9, 6, 5, 4, 3, 2], 5)]
     for dataList, partition_value in testCases:
         nodeList = Node.fromList(dataList)
         partitionedList = Node.toList(
             LinkedListQuestions.partition(nodeList, 5))
         self.check_if_list_partitioned(partitionedList, partition_value)
 def InsertAtBeginning(self, data):
     if self.head == None:
         print("EMpty linked list.")
     else:
         new = Node(data)
         new.next = self.head
         self.head = new
Пример #10
0
def sumLists(L1, L2):
    if not L1 and not L2: 
        return False
    if L1: 
        curr1 = L1.head
    if L2: 
        curr2 = L2.head
    output = LinkedList()
    carry = 0

    while curr1 or curr2:
        if not output.head:
            output.head = Node(0)
            curr3 = output.head 
        else:
            curr3.next = Node(0)
            curr3 = curr3.next 
        if curr1:
            curr3.value += curr1.value
        if curr2:
            curr3.value += curr2.value
        curr3.value += carry
        carry = 0
        if curr3.value > 9:
            curr3.value = curr3.value % 10
            carry = 1
        if curr1:
            curr1 = curr1.next
        if curr2:
            curr2 = curr2.next
    
    return output
Пример #11
0
def test_deleting_single_value():
    linked_list = LinkedList()

    linked_list.delete(0)
    assert linked_list.head is None
    assert linked_list.tail is None

    # single-element list
    linked_list.add_in_tail(Node(1))

    linked_list.delete(1)
    assert linked_list.len() == 0
    assert linked_list.head is None
    assert linked_list.tail is None

    # multi-element list
    n1 = Node(1)
    n2 = Node(2)
    n3 = Node(3)
    n4 = Node(3)
    linked_list.add_in_tail(n1)
    linked_list.add_in_tail(n2)
    linked_list.add_in_tail(n3)
    linked_list.add_in_tail(n4)

    linked_list.delete(3)
    assert linked_list.head == n1
    assert linked_list.tail == n4
    assert linked_list.len() == 3
Пример #12
0
def sum(ll1=LinkedList(), ll2=LinkedList()):
    result_list = LinkedList()
    summing_list, summed_list = swap_lists(ll1, ll2)

    carry = 0
    c1, c2 = summing_list.head, summed_list.head
    while c1 and c2:
        sum = c1.value + c2.value + carry
        if sum >= 10:
            sum -= 10
            carry = 1
        else:
            carry = 0
        result_list.append(Node(sum))
        c1 = c1.next_node
        c2 = c2.next_node

    while c2:
        sum = c2.value + carry
        if sum >= 10:
            sum -= 10
            carry = 1
        else:
            carry = 0
        result_list.append(Node(sum))
        if not c2.next_node and carry == 1:
            result_list.append(Node(carry))
        c2 = c2.next_node

    result_list.print_list()
Пример #13
0
def main():
    # Build lists

    list_head = Node('genesis')
    ptr = list_head
    for b in range(50):
        ptr.next = Node(b)
        ptr = ptr.next

    # Copy and create circular list
    list_cir = copy.deepcopy(list_head)
    cir_ptr = list_cir
    while cir_ptr.next is not None:
        cir_ptr = cir_ptr.next

    cir_ptr.next = list_cir

    #p(list_cir)  # 1 Infinity Loop.... Cupertino California

    # Check if linked list is circular.
    print('CIR? %s' % is_circular(list_cir))
    print('TO REVERSE: ')
    p(list_head)
    print('IN REVERSE: ')
    p(Reverse(list_head))

    print('Nth to the Last')
    print(NthToLast(15, list_head))
def main():
    new_queue = custom_queue_stack()
    one = Node(1)
    two = Node(2)
    three = Node(3)
    new_queue.enqueue(one)
    new_queue.enqueue(two)
    print new_queue.peek().content
Пример #15
0
    def test_delete_middle(self):
        testNodes = [[1, 2, 3, 4, 5], [1, 2, 3, 4], [1, 2, 3, 4, 5, 6, 7]]
        for nodeList in testNodes:
            testNode = Node.fromList(nodeList)
            self.list_delete_middle(nodeList)
            LinkedListQuestions.delete_middle(testNode)

            self.assertListEqual(nodeList, Node.toList(testNode))
Пример #16
0
def pad_zero(n, num):
    new_head = n
    next_node = n
    for _ in range(num):
        new_head = Node(0)
        new_head.next = next_node
        next_node = new_head
    return new_head
def naiveFillSmallLinkedList(val, nodeList, node):
    if node is None:
        node = Node(val)
        nodeList.head = node
    else:
        temp = Node(val)
        node.next = temp
        node = node.next
    return node, nodeList
Пример #18
0
def append_to_node(data, n: LinkedList.Node, val=None):
    if type(data) == int:
        n.val = data
        return n.next
    elif type(data) == LinkedList.Node:
        n.val = val
        n.next = data if data.val else None
        return data
    return None
Пример #19
0
 def push(self, v):
     temp = Node(v)
     if self.top:
         temp.next = self.top
         self.top = temp
     else:
         self.top = temp
         self.bottom = temp
     self.length += 1
Пример #20
0
def add_lists_helper(n1, n2):
    if n1 is None and n2 is None:
        return None, 0

    n, carry = add_lists_helper(n1.next, n2.next)
    val = carry + n1.data + n2.data
    new = Node(val % 10)
    new.next = n

    return new, 1 if val >= 10 else 0
    def ordered_insertion(self, data):
        current = self.head
        previous = None
        new_node = Node(data)
        while current and current.data < data:
            previous = current
            current = current.next_node

        new_node.next_node = previous.next_node
        previous.next_node = new_node
Пример #22
0
    def enQueue(self, data):
        if self.isFull():
            print "Queue OverFlow : Queue is Full!"
            return

        node = Node(data)
        self.head.next = node
        node.prev = self.head
        self.head = node
        self.numElements = self.numElements + 1
Пример #23
0
    def test_2(self):
        n1 = Node(1)
        n2 = Node(2)
        n3 = Node(3)

        ll = LinkedList()
        ll.set_head(n1)
        ll.append_to_tail(n2, n3)

        assert loop_detection(ll) is None
def reverseLinkedList(head):
    current = head
    prev = Node(None)
    prev.next = current
    while current:
        head = head.next
        current.next = prev
        prev = current
        current = head
    return prev
Пример #25
0
 def add(self, value):
     if (not self.first):
         self.first = Node(value)
         return True
     else:
         current = self.first
         while (current.next):
             current = current.next
         current.next = Node(value)
         return True
 def InsertAtEnd(self, data):
     if self.head == None:
         print("Empty linked list.")
     else:
         ptr = self.head
         while ptr.next != self.head:
             ptr = ptr.next
         new = Node(data)
         ptr.next = new
         new.next = self.head
Пример #27
0
def main():
    one = Node(2)
    two = Node(3)
    three = Node(4)
    one.next = two
    list_one = CustomLinkedList()
    list_one.insert_at_the_beginning(one)
    list_one.insert_at_the_beginning(two)
    list_one.insert_at_the_beginning(three)
    four = find_start_of_loop(list_one)
Пример #28
0
def test_clean():
    linked_list = LinkedList()
    linked_list.add_in_tail(Node(1))
    linked_list.add_in_tail(Node(2))
    linked_list.add_in_tail(Node(3))
    linked_list.add_in_tail(Node(4))

    linked_list.clean()

    assert linked_list.head is None
    assert linked_list.tail is None
Пример #29
0
 def test_loop_detection_runner_find_first_element(self):
     ll = LinkedList()
     head = Node(0)
     ll.head = head
     n = ll.head
     for i in range(1, 5):
         n.next = Node(i)
         n = n.next
     n.next = ll.head
         
     self.assertEqual(0, loop_detection_runner(ll).data)
Пример #30
0
 def test_loop_detection_true(self):
     ll = LinkedList()
     head = Node(0)
     ll.head = head
     n = ll.head
     for i in range(1, 5):
         n.next = Node(i)
         n = n.next
     n.next = ll.head
         
     self.assertTrue(loop_detection(ll))
Пример #31
0
def sumIt(n1, n2, carry):
  if (n1 is None) and (n2 is None):
    return None
  result = Node()
  v = carry
  if n1 is not None:
    v += n1.value
  if n2 is not None:
    v += n2.value
  result.value = v%10
  next = sumIt(n1.next, n2.next, v/10)
  result.next = next
  return result
Пример #32
0
        def testFindLoop(self):

                list = List()
                a = Node('A')
                b = Node('B')
                c = Node('C')
                d = Node('D')
                e = Node('E')

                list.head = a
                a.next = b
                b.next = c
                c.next = d
                d.next = e
                e.next = c

                self.assertEqual(findLoop(list), c)
Пример #33
0
def add_two_list_recursive(list_1,list_2,carry=0):
    if not list_1 and not list_2:
        if carry > 0:
            return Node(carry)
        return None
    else:
        value = carry
        result_node = Node()
        if list_1:
            value += list_1.data
        if list_2:
            value += list_2.data
        if value >= 10:
            carry = 1
        else:
            carry = 0
        result_node.data = value % 10
        next_node = add_two_list_recursive(list_1.next,list_2.next,carry)
        result_node.next = next_node
        return result_node
Пример #34
0
def rotate_llist(head, k):
    dummy = Node(-1, next=head)
    # count how many nodes in the list
    len = 1
    last = head # find the last node
    while last.next:
        len += 1
        last = last.next
    k = k % len
    if k == 0: return dummy.next
    
    # reset
    prev = dummy
    head = dummy.next
    # point to the new head and the previous node
    for _ in range(len-k):
        prev = head
        head = prev.next
    prev.next = last.next
    last.next = dummy.next
    dummy.next = head
    return dummy.next
Пример #35
0
    if len(kthArray) == k:
      kthArray.insert(0, node.data)
      kthArray.pop(k)
    else:
      kthArray.insert(0, node.data)

    node = node.next

  print kthArray
  if len(kthArray) != k:
    print "List too short!!"
    return None

  return kthArray[k-1]

node1 = Node(1)
node2 = Node(2)
node1.next = node2
node3 = Node(3)
node2.next = node3
node4 = Node(4)
node3.next = node4
node5 = Node(5)
node4.next = node5
node6 = Node(6)
node5.next = node6
node7 = Node(7)
node6.next = node7

print_list(node1)
print findKthLastNode(node1, 4)
Пример #36
0
  def test_find_loop_node(self):
    n1 = Node(1)
    n2 = Node(2)
    n3 = Node(3)
    n4 = Node(4)
    n5 = Node(5)
    n6 = Node(6)
    n7 = Node(7)
    n8 = Node(8)
    n9 = Node(9)
    n10 = Node(10)
    n11 = Node(11)

    l1 = LinkedList()
    l1.head = n1
    n1.next = n2
    n2.next = n3
    n3.next = n4
    n4.next = n5
    n5.next = n6
    n6.next = n7
    n7.next = n8
    n8.next = n9
    n9.next = n10
    n10.next = n11
    n11.next = n6
    actual = find_loop_node(l1)
    self.assertEqual(actual, n6)

    l1 = LinkedList()
    l1.head = n1
    n1.next = n2
    n2.next = n3
    n3.next = n4
    n4.next = n5
    n5.next = n6
    n6.next = n7
    n7.next = n8
    n8.next = n9
    n9.next = n10
    n10.next = n11
    n11.next = n8
    actual = find_loop_node(l1)
    self.assertEqual(actual, n8)
Пример #37
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Implement an algorithm to delete a node in the middle of a single linked list,
# given only access to that node.

# example:
# Input: the node 'c' from the linked list a->b->c->d->e->None
# Result: a->b->d->e->None

from LinkedList import Node

v = Node(value=1, next=Node(value=2, next=Node(value=3, next=Node(value=4,
    next=Node(value=5, next=Node(value=6))))))

def delThisNode(node):  
  node.value = node.next.value
  aux = node.next
  node.next = aux.next
  del aux

nodeToDelete = v.next.next.next

v.printAll()
delThisNode(nodeToDelete)
v.printAll()
Пример #38
0
            return longNode
        else:
            longNode = longNode.next   
            shortNode = shortNode.next

    return False

def getListLength(node):
    counter = 0
    while node:
        counter += 1
        node = node.next

    return counter

nodeOne1 = Node(7)
nodeOne2 = Node(1)
nodeOne1.next = nodeOne2
nodeOne3 = Node(6)
nodeOne2.next = nodeOne3

nodeTwo1 = Node(5)
nodeTwo2 = Node(9)
nodeTwo1.next = nodeTwo2
nodeTwo3 = Node(2)
nodeTwo2.next = nodeTwo3

nodeOne1.next = nodeTwo1


answerNode = returnIntersectingNode(nodeOne1, nodeTwo1)