예제 #1
0
 def test1(self):
     self.assertEqual(
         self.S.addTwoNumbers(
             ListNode.linkedList('2->4->3'), ListNode.linkedList('5->6->4')
         ),
         ListNode.linkedList('7->0->8')
     )
예제 #2
0
 def test2(self):
     self.assertEqual(
         self.S.addTwoNumbers(
             ListNode.linkedList('5'), ListNode.linkedList('5')
         ),
         ListNode.linkedList('0->1')
     )
    def __init__(self, dataStructure):
        super(interactiveDataStructures, self).__init__()
        # rich module elements
        pretty.install()
        traceback.install()
        self.console = Console()
        # Datastructure elements
        availableDataStrutuces = {
            'DynamicArray': DynamicArray(),
            'SingleLinkedList': SinglyLinkedList(),
            'DoublyLinkedList': DoublyLinkedList(),
            'Stack': Stack(),
            'Queue': Queue(),
            'PriorityQueue': PriorityQueue()
            }

        correspondingNodes = {
            'DynamicArray': None,
            'SingleLinkedList': ListNode(None),
            'DoublyLinkedList': ListNode(None),
            'Stack': StackNode(None),
            'Queue': QueueNode(None),
            'PriorityQueue': PQNode(None)
            }

        if dataStructure in availableDataStrutuces:
            self.dataStructure = availableDataStrutuces[dataStructure]
            self.DSNode = correspondingNodes[dataStructure]
            self.DSname = dataStructure
            interactiveDataStructures.prompt = text.Text(f'({dataStructure}) ', style="bold magenta") # doesn't quite work
        else:
            raise ValueError(f'Please choose one of the following available data structure: {availableDataStrutuces.keys()}')
예제 #4
0
 def test1(self):
     self.assertEqual(
         self.S.mergeKLists([
             ListNode.linkedList('1->4->5'),
             ListNode.linkedList('1->3->4'),
             ListNode.linkedList('2->6')
         ]), ListNode.linkedList('1->1->2->3->4->4->5->6'))
예제 #5
0
 def deleteNode(self, node: ListNode):
     """
     :type node: ListNode
     :rtype: void Do not return anything, modify node in-place instead.
     """
     node.val = node.next.val
     node.next = node.next.next
예제 #6
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        # remainder of dividing by 10
        rem = 0
        root = None
        tail = None
        while (l1 or l2):
            sum = rem
            if l1:
                sum += l1.val  
                l1 = l1.next
            if l2:
                sum += l2.val
                l2 = l2.next

            if sum >= 10:
                rem = 1
                node = ListNode(sum % 10)
            else:
                rem = 0
                node = ListNode(sum)
                
            if root:
                tail.next = node
                tail = node
            else:
                root = node
                tail = node
                
        if rem > 0:
            tail.next = ListNode(rem)        
        return root
예제 #7
0
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        head = None
        tail = None
        minHeap = MinHeap([node for node in lists if node is not None])
        while True:
            min = minHeap.min()
            if min is None:
                return head

            if head is None:
                head = ListNode(min.val)
                tail = head
            else:
                tail.next = ListNode(min.val)
                tail = tail.next
            if min.next is None:
                minHeap.deleteMin()
            else:
                minHeap.replaceMin(min.next)
예제 #8
0
    def enqueue(self, item):

        n = ListNode(item)
        if self.isEmpty():
            self._qtail = n
            self._qhead = n

        else:
            self._qtail.next = n
            self._qtail = n

        self._count += 1
예제 #9
0
    def enqueue(self, item):
        newNode = ListNode(item)

        if self._count == 0:
            self._qhead = newNode
            self._qtail = newNode
            self._count += 1
        else:
            x = self._qtail
            x.next = newNode
            self._qtail = newNode
            self._count += 1
예제 #10
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        first = (l1.val + l2.val) % 10
        addOne = int((l1.val + l2.val) / 10)
        head = ListNode(first)
        current = head
        nextNode = None
        print("first ", first)
        print("addOne", addOne)
        l1 = l1.next
        l2 = l2.next
        while l1 or l2 or addOne == 1:
            val1 = 0 if l1 == None else l1.val
            val2 = 0 if l2 == None else l2.val
            first = (val1 + val2 + addOne) % 10

            print("val1 , val2, addOne", val1, val2, first)
            newNode = ListNode((val1 + val2 + addOne) % 10)
            addOne = int((val1 + val2 + addOne) / 10)
            current.next = newNode
            current = newNode
            l1 = None if l1 == None else l1.next
            l2 = None if l2 == None else l2.next

        return head
예제 #11
0
def mergeTwoLists(A, B):
    if A == None:
        return B
    if B == None:
        return A
    first = A
    second = B
    if A.val <= B.val:
        out = ListNode(A.val)
        first = first.next
    else:
        out = ListNode(B.val)
        second = second.next

    temp = out
    while first != None and second != None:
        if first.val <= second.val:
            new = ListNode(first.val)
            temp.next = new
            temp = temp.next
            first = first.next
        else:
            new = ListNode(second.val)
            temp.next = new
            temp = temp.next
            second = second.next
    while first != None:
        new = ListNode(first.val)
        temp.next = new
        temp = temp.next
        first = first.next

    while second != None:
        new = ListNode(second.val)
        temp.next = new
        temp = temp.next
        second = second.next

    return out
예제 #12
0
        print("addOne", addOne)
        l1 = l1.next
        l2 = l2.next
        while l1 or l2 or addOne == 1:
            val1 = 0 if l1 == None else l1.val
            val2 = 0 if l2 == None else l2.val
            first = (val1 + val2 + addOne) % 10

            print("val1 , val2, addOne", val1, val2, first)
            newNode = ListNode((val1 + val2 + addOne) % 10)
            addOne = int((val1 + val2 + addOne) / 10)
            current.next = newNode
            current = newNode
            l1 = None if l1 == None else l1.next
            l2 = None if l2 == None else l2.next

        return head


l1 = ListNode(2)
l1.append([4, 3])
l1.printLL()
l2 = ListNode(5)
l2.append([6, 4])
l2.printLL()

s = Solution()

r = s.addTwoNumbers(l1, l2)
r.printLL()
 def test_length(self):
     input = [1, 1000, 8, 9, 'abba']
     linked_list = ListNode.build(input)
     output = linked_list.length()
     self.assertEqual(5, output)
 def test_all_unique_false(self):
     input = [5, 6, 7, 8, 'z', 8]
     linked_list = ListNode.build(input)
     self.assertFalse(linked_list.all_unique())
예제 #15
0
 def test5(self):
     self.assertNotEqual(ListNode.linkedList(
         '2->5->3'), ListNode.linkedList('2->4->3'), '2->5->3 != 2->4->3')
 def test_is_circular_false_even(self):
     head = ListNode.build([i for i in range(10)])
     self.assertFalse(head.is_circular())
예제 #17
0
            return head
        elif not fast and counter < k:

            k = k % counter
            counter = 0
            fast = head
            if k == 0:
                return head
            while counter < k and fast:
                fast = fast.next
                counter += 1

        while fast.next:
            fast = fast.next
            slow = slow.next
        fast.next = head
        newHead = slow.next
        slow.next = None
        return newHead


l1 = ListNode()
l1.create_linklist([1, 2, 3, 4, 5])
k = 11
l1.printLL()
s = Solution()

for i in range(20):
    r = s.rotateRight(l1, i)
    r.printLL()
예제 #18
0
 def test1(self):
     self.assertEqual(str(ListNode(2, ListNode())), '2->0', '2->0')
 def test_all_unique_true(self):
     input = ['a', 'b', 'c', 'aa', 11, 1]
     linked_list = ListNode.build(input)
     self.assertTrue(linked_list.all_unique())
예제 #20
0
 def push(self, item):
     newNode = ListNode(item)
     newNode.next = self._top
     self._top = newNode
     self._size += 1
예제 #21
0
 def test_1(self):
     list = ListNode.linkedList('4->5->1->9')
     self.solution.deleteNode(list)
     self.assertEqual(str(list), '5->1->9')
 def test_no_to_list_when_circular(self):
     head = ListNode.build_circular([i for i in range(21)])
     self.assertFalse(head.to_list())
예제 #23
0
#         self.next = next
from linkedList import ListNode


class Solution:
    def oddEvenList(self, head: ListNode) -> ListNode:
        if not head:
            return None
        odd = head
        even = head.next
        evenHead = even
        while even and even.next:

            odd.next = even.next
            odd = even.next
            even.next = odd.next
            even = odd.next

        odd.next = evenHead
        return head


head = ListNode(2)
head.append([1, 3, 5, 6, 4, 7])
head.printLL()

s = Solution()

r = s.oddEvenList(head)
r.printLL()
 def test_is_circular_one_node(self):
     head = ListNode.build([1])
     self.assertFalse(head.is_circular())
예제 #25
0
#https://leetcode.com/problems/middle-of-the-linked-list/
from linkedList import ListNode


class Solution:
    def middleNode(self, head: ListNode) -> ListNode:
        if not head:
            return None
        slow, fast = head, head
        while fast:
            fast = fast.next
            if not fast:
                return slow
            fast = fast.next
            slow = slow.next

            if not fast:
                return slow

        return slow


head = ListNode(1)
head.append([2, 3])
head.printLL()

s = Solution()

r = s.middleNode(head)
print(r.val)
예제 #26
0
 def test2(self):
     self.assertEqual(
         str(ListNode(2, ListNode(0, ListNode(3)))), '2->0->3', '2->0->3')
 def test_to_set(self):
     input = [1, 'a', 2, 'x', 'x']
     linked_list = ListNode.build(input)
     output = linked_list.to_set()
     self.assertEqual({1, 'a', 2, 'x'}, output)
예제 #28
0
 def test3(self):
     self.assertEqual(str(ListNode(2)), '2')
예제 #29
0
 def test4(self):
     self.assertEqual(ListNode.linkedList('2->4->3'),
                      ListNode.linkedList('2->4->3'), '2->4->3')
 def test_is_circular_true(self):
     head = ListNode.build_circular([i for i in range(666)])
     self.assertTrue(head.is_circular())