Example #1
0
 def test_insert(self):
     test_list = LinkedList(ListNode(3))
     test_list.prepend(2)
     test_list.prepend(4)
     test_list.insert(1, 2)
     self.assertEqual(test_list.head.value, 4, "1 is the head")
     self.assertEqual(test_list.head.next_node.next_node.value, 1, "1 is the 3rd node value")
Example #2
0
 def test_create_linked_list(self):
     length = randint(0, 30)
     print("<b>Input data:</b>")
     print("Random Length = {}<br><hr>".format(length))
     linked_list = LinkedList(length)
     print("Create list:<br>")
     print("LinkedList.length = {}<br>".format(len(linked_list)))
     print("<b>{}</b><br><br><hr>".format(linked_list.__str__()))
     result = len(linked_list) == length
     print("LinkedList.length == Random Length, <b>Result = {}</b>".format(
         result))
     self.assertTrue(
         result,
         "Can not create List with length = {}, Current length = {}".format(
             length, len(linked_list)))
Example #3
0
    def __init__(self, vertices):
        self.vertices = vertices
        self.array = []

        for i in range(vertices):
            temp = LinkedList()
            self.array.append(temp)
Example #4
0
    def test_find_intersect_method_2(self):
        length1 = randint(1, 30)
        length2 = randint(1, 30)
        print("<b>Input data:</b>")
        print("Random Length1 = {}, Length2 = {}<br><hr>".format(
            length1, length2))
        linked_list1 = LinkedList(length1)
        print("Create list1:<br>")
        print("LinkedList1.length = {}<br>".format(len(linked_list1)))
        print("<b>{}</b><br><br><hr>".format(linked_list1.__str__()))
        result1 = len(linked_list1) == length1
        print(
            "LinkedList1.length == Random Length1, <b>Result = {}</b><br><hr>".
            format(result1))
        self.assertTrue(
            result1,
            "Can not create List1 with length1 = {}, Current length = {}".
            format(length1, len(linked_list1)))

        linked_list2 = LinkedList(length2)
        print("Create list2:<br>")
        print("LinkedList2.length = {}<br>".format(len(linked_list2)))
        print("<b>{}</b><br><br><hr>".format(linked_list2.__str__()))
        result2 = len(linked_list2) == length2
        print(
            "LinkedList2.length == Random Length2, <b>Result = {}</b><br><hr>".
            format(result2))
        self.assertTrue(
            result2,
            "Can not create List2 with length2 = {}, Current length = {}".
            format(length1, len(linked_list1)))

        print("Update list 2:<br>")
        node_index_insert = randint(0, length1 - 1)
        noge_index_change = randint(0, length2 - 1)

        print("List1: Node index to insert = {}<br>".format(node_index_insert))
        print("List2: Node index to change = {}<br><br>".format(
            noge_index_change))

        print("Insert node({}, {})<br>".format(
            linked_list1[node_index_insert].data,
            linked_list2[noge_index_change].data))

        insert_node(linked_list1[node_index_insert],
                    linked_list2[noge_index_change])

        print("<br>List1: <b>{}</b><br><br>".format(linked_list1.__str__()))
        print("List2: <b>{}</b><br><br><hr>".format(linked_list2.__str__()))

        result = find_intersect2(linked_list1[0], linked_list2[0])
        print("<br>Counter = {}<br>".format(result))
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """

        pA = headA
        pB = headB

        while pA is not pB:
            pA = headB if pA is None else pA.next
            pB = headA if pB is None else pB.next

        return pA


sol = Solution()
node1 = LinkedList(4)
node1.next = LinkedList(1)
node1.next.next = LinkedList(8)
node1.next.next.next = LinkedList(4)
node1.next.next.next.next = LinkedList(5)

node2 = LinkedList(5)
node2.next = LinkedList(0)
node2.next.next = LinkedList(1)
node2.next.next.next = LinkedList(8)
node2.next.next.next.next = LinkedList(4)
node2.next.next.next.next.next = LinkedList(5)

print(sol.getIntersectionNode(node1, node2))
from linkedlist.LinkedList import LinkedList

linkedlist = LinkedList()
linkedlist.insertStart(12)
linkedlist.insertStart(25)
linkedlist.insertStart(24)
linkedlist.insertEnd(16)
linkedlist.insertStart(13)
linkedlist.traverseList()
Example #7
0
 def test_search(self):
     test_list = LinkedList(ListNode(3))
     test_list.prepend(2)
     test_list.prepend(5)
     self.assertEqual(test_list.search(3), True, "3 is in this list")
Example #8
0
 def test_prepend(self):
     test_list = LinkedList(ListNode(3))
     self.assertEqual(test_list.head.value, 3, "head value should be 3")
     test_list.prepend(2)
     self.assertEqual(test_list.head.value, 2, "new head value should be 2")
Example #9
0
__author__ = 'lisa'

from linkedlist.LinkedList import LinkedList

newlinkedlist = LinkedList()
newlinkedlist.insertStart(21)
newlinkedlist.insertStart(23)
newlinkedlist.insertStart(25)
newlinkedlist.insertStart(30)
newlinkedlist.insertStart(35)
newlinkedlist.traverseList()
newlinkedlist.remove(35)
print("second run")
newlinkedlist.insertStart(37)
newlinkedlist.traverseList()
from linkedlist.LinkedList import LinkedList

mylist = LinkedList()
mylist.insertlast(12)
mylist.insertfirst(121)
mylist.insertlast(4)
mylist.insertfirst(81)
mylist.insertlast(1)
mylist.insertlast(2)
mylist.insertlast(3)
mylist.insertlast(4)

mylist.traverselist()
size = mylist.size()
print("size is : ", size)

mylist.search(1)
mylist.search(4)

mylist.remove(121)
mylist.traverselist()
size = mylist.size()
print("size is : ", size)

mylist.remove(121)
mylist.traverselist()
size = mylist.size()
print("size is : ", size)

mylist.remove(2)
mylist.traverselist()