Пример #1
0
    def test_noChange(self):
        l = LinkedList(6)
        n1 = Node(7)

        l.addNode(n1)

        self.assertTrue(l.equals(removeDuplicatesWithBuffer(l)))
	def test_unbalanced_l1(self):
		l1 = LinkedList(6)
		l1.addNode(Node(7))
		l2 = LinkedList(6)

		ret = LinkedList(2)
		ret.prepend(Node(8))
		
		self.assertTrue(ret.equals(sumReversedLinkedLists(l1, l2)))
Пример #3
0
    def test_list_all_same(self):
        l = LinkedList(6)
        n1 = Node(6)
        n2 = Node(6)

        l.addNode(n1)
        l.addNode(n2)

        self.assertEquals(1, removeDuplicatesWithBuffer(l).getLength())
Пример #4
0
    def test_three_nodes_middle(self):
        l1 = LinkedList(6)
        n = Node(7)
        n2 = Node(8)
        l1.addNode(n)
        l1.addNode(n2)

        ret = deleteGivenNode(l1.getHead().getNext(), l1)
        self.assertEquals(2, l1.getLength())
        self.assertEqual(6, l1.getHead().getNodeVal())
        self.assertEqual(8, l1.getHead().getNext().getNodeVal())
Пример #5
0
    def test_list_one_repeat(self):
        l = LinkedList(6)
        n1 = Node(7)
        n2 = Node(6)

        l.addNode(n1)
        l.addNode(n2)
        ans = removeDuplicatedWithoutBuffer(l)
        self.assertEquals(2, ans.getLength())
        arr = [6, 7]

        head = ans.getHead()
        i = 0
        while head.hasNext():
            self.assertEquals(arr[i], head.getNodeVal())
            i = i + 1
            head = head.getNext()
	def test_singleton(self):
		l1 = LinkedList(6)
		l2 = LinkedList(6)

		ret = LinkedList(2)
		ret.prepend(Node(1))
		
		self.assertTrue(ret.equals(sumReversedLinkedLists(l1, l2)))
Пример #7
0
    def test_two_nodes(self):
        l1 = LinkedList(6)
        n = Node(7)
        l1.addNode(n)

        ret = deleteGivenNode(l1.getHead(), l1)
        self.assertEquals(1, l1.getLength())
        self.assertEqual(7, l1.getHead().getNodeVal())
	def test_book_sample(self):
		l1 = LinkedList(3)
		l1.addNode(Node(1))
		l1.addNode(Node(5))
		l2 = LinkedList(5)
		l2.addNode(Node(9))
		l2.addNode(Node(2))

		ret = LinkedList(8)
		ret.addNode(Node(0))
		ret.addNode(Node(8))

		self.assertTrue(ret.equals(sumReversedLinkedLists(l1, l2)))
def sumReversedLinkedLists(l1, l2):
	if l1 is None or l2 is None:
		return LinkedList(None)

	# initial computation to deal with annoying constructor
	curr1 = l1.getHead()
	curr2 = l2.getHead()
	ret = LinkedList(None)
	to_add = 0
	while curr1 is not None and curr2 is not None:
		val1 = curr1.getNodeVal()
		val2 = curr2.getNodeVal()

		if val1 + val2 + to_add >= 10:
			new_node = Node(val1 + val2 - 10)
			to_add = 1
		else:
			new_node = Node(val1 + val2 + to_add)
			to_add = 0

		ret.prepend(new_node)
		curr1 = curr1.getNext()
		curr2 = curr2.getNext()


	if curr1 is None and curr2 is not None:
		while curr2 is not None:
			val = curr2.getNodeVal()
			if val + to_add >= 10:
				new_node = Node(val + to_add - 10)
				to_add = 1
			else:
				new_node = Node(val + to_add)
				to_add = 0
			ret.prepend(new_node)
			curr2 = curr2.getNext()

	if curr1 is not None and curr2 is None:
		while curr1 is not None:
			val = curr1.getNodeVal()
			if val + to_add >= 10:
				new_node = Node(val + to_add - 10)
				to_add = 1
			else:
				new_node = Node(val + to_add)
				to_add = 0
			ret.prepend(new_node)
			curr1 = curr1.getNext()

	if to_add == 1:
		ret.prepend(Node(1))

	return ret
Пример #10
0
    def test_singleton_does_nothing(self):
        l1 = LinkedList(6)

        ret = deleteGivenNode(l1.getHead(), l1)
        self.assertEquals(1, l1.getLength())