def sumLists(list1Node, list2Node, carry=0):
    if not (list1Node or list2Node or carry > 0):
        return None

    sumNode = Node(carry)
    list1Next = None
    list2Next = None

    # Add the listNode only if necessary, this handles varying lengths
    if list1Node:
        sumNode.value += list1Node.value
        list1Next = list1Node.next
    if list2Node:
        sumNode.value += list2Node.value
        list2Next = list2Node.next

    carry = sumNode.value / 10
    sumNode.value = sumNode.value % 10

    # hands off carry and next nodes of both lists recursively
    nextSumNode = sumLists(list1Next, list2Next, carry)
    # After node is received, take current node and put in front
    sumNode.next = nextSumNode

    return sumNode
Exemplo n.º 2
0
def main():
    ########## testing single linked list
    single_linked_list = SingleyLinkedList(Node(1))
    single_linked_list.add_node(Node(2))
    single_linked_list.add_node(Node(3))
    single_linked_list.add_node(Node(4))
    # single_linked_list.print()
    # print(single_linked_list.tail.get_value())
    # print(single_linked_list.cycle_check())
    # single_linked_list.tail.next_node=single_linked_list.tail
    # print(single_linked_list.cycle_check())
    # single_linked_list.reverse_list()
    #single_linked_list.print()
    print(single_linked_list.nth_to_last_node(1))
    single_linked_list.print()
    print(single_linked_list.nth_to_last_node(3))
    def test_index_of(self):
        self.myList = LinkedList()
        node1 = Node(1)
        node2 = Node(2)
        node3 = Node(3)
        self.myList.head = node1
        node1.next = node2
        node2.next = node3
        
        self.assertEqual(self.myList.index_of(1), 0)
        self.assertEqual(self.myList.index_of(2), 1)
        self.assertEqual(self.myList.index_of(3), 2)

        self.assertFalse(self.l1.index_of(1))
        self.assertFalse(self.l2.index_of(1))
        self.assertEqual(self.l3.index_of(1), 0)
Exemplo n.º 4
0
def recur_reverse(node: Node) -> Node:
    """Reverse a given linked list in-place.

    :param l: A singly linked list class object
    :type l: SinglyLinkedList
    """
    if node is None or node.next is None:
        return node
    new_node = recur_reverse(node.next)
    node.next.next = node
    node.next = None
    return new_node
Exemplo n.º 5
0
def remove_duplicates_sorted_ii(llist):

    sentinel = Node(0, llist.head)
    
    pred = sentinel
    
    head = llist.head
    while head:
        
        if head.next and head.val == head.next.val:
            while head.next and head.val == head.next.val:
                head = head.next
            
            pred.next = head.next
        else:
            pred = pred.next
        
        head = head.next
    
    llist.head = sentinel.next
    return
class NodeFunctionsTest(unittest.TestCase):

    def setUp(self):
        self.n1 = Node(1)
        self.n2 = Node(2)
        self.n3 = Node(3)
        self.n3.next = self.n2

    def test_get_data(self):
        self.assertEqual(self.n1.get_data(), 1)
        self.assertEqual(self.n3.get_data(), 3)

    def test_get_next(self):
        self.assertEqual(self.n1.get_next(), None)
        self.assertEqual(self.n3.get_next(), self.n2)

    def test_set_data(self):
        self.n1.set_data(11)
        self.assertEqual(self.n1.data, 11)
    
    def test_set_next(self):
        self.n2.set_next(self.n1)
        self.assertEqual(self.n2.next, self.n1)
Exemplo n.º 7
0

def intersects(ll1, ll2):
    if ll1.getHead() == None or ll2.getHead() == None:
        return False
    current = ll1.getHead()
    aux = {}
    while current != None:
        aux[current] = current
        current = current.get_next()
    current = ll2.getHead()
    while current != None:
        if current in aux.keys():
            return True
        current = current.get_next()
    return False


ll1 = SinglyLinkedList()
inter = Node(5)
ll1.insert_node_at_beginning(inter)
ll1.insert_at_beginning(5)
ll1.insert_at_beginning(6)

ll2 = SinglyLinkedList()
ll2.insert_at_beginning(5)
ll2.insert_at_beginning(3)
ll2.insert_at_beginning(4)

print(intersects(ll1, ll2))
Exemplo n.º 8
0
        self.first = new_node
 
    def remove(self, node):
        if self.first.next == self.first:
            self.first = None
        else:
            node.prev.next = node.next
            node.next.prev = node.prev
            if self.first == node:
                self.first = node.next
    def display(self):
        if self.first is None:
            return
        current = self.first
        while True:
            print(current.data, end = ' ')
            current = current.next
            if current == self.first:
                break  

list = CircularDoublyLinkedList()
n1 = Node("Melanie")
n2 = Node("Andrew")
n3 = Node("Ronaldo")
n4 = Node("Mister")
list.insert_at_beg(n1)
list.insert_at_beg(n2)
list.insert_at_end(n4)
list.insert_at_end(n3)
list.display()
 def setUp(self):
     self.n1 = Node(1)
     self.n2 = Node(2)
     self.n3 = Node(3)
     self.n3.next = self.n2
        Implement an algorithm to delete a node in the middle of 
        a singly linked list given access only to that node
"""
from SinglyLinkedList import Node

#A cute trick, just copies the next node in the list
#removes the next node in the list, "deletes" itself
def removeNode(node):
    if node.next:
        node.value = node.next.value
        node.next = node.next.next
    else:
        return None


headNode = Node(1, Node(2,
                    Node(3, 
                        Node(4,
                            Node(5,
                                Node(6))))))

middleNode = headNode

for x in xrange(3):
    middleNode = middleNode.next
print middleNode

headNode.printList()
removeNode(middleNode)
headNode.printList()
Exemplo n.º 11
0
	slow = linkedList._head
	fast = linkedList._head
	while slow is not None and slow._next is not None:
		slow = slow._next
		if fast is not None: 
			fast = fast._next
			fast = fast._next
		else: 
			return False 
		if slow == fast:
			return True
	return False


ll = SinglyLinkedList()
n5 = Node(5)
n4 = Node(4, n5)
n3 = Node(3, n4)
n2 = Node(2, n3)
n1 = Node(1, n2)

n5._next = n3

# setting the head to the created linked list
ll._head = n1

# testing to see if the linked list loops
curr = ll._head
for i in range(8):
  print curr._element
  curr = curr._next