def main(): # Create first node and set the head ptr testListHead = testList = ListNode(3) # Add the second node and set a ptr that the end of the list can loop back to testList.next = ListNode(2) testList = testList.next loopback = testList # Populate the remaining nodes in the linked list testList.next = ListNode(0) testList = testList.next testList.next = ListNode(-4) testList = testList.next testList.next = loopback # Set ptr to beginning of list to pass to Solution class testList = testListHead # Instantiate Solution class s = Solution() # Run the solution answer = s.hasCycle(testList) print("The linked list contains a cycle: ") print(answer)
def list_to_LL(arr): if len(arr) < 1: return None if len(arr) == 1: return ListNode(arr[0]) return ListNode(val=arr[0], next=list_to_LL(arr[1:]))
def create_node(nums: list) -> ListNode: top = cur = ListNode(nums[0]) for n in nums[1:]: cur.next = ListNode(n) cur = cur.next return top
def test_something(self): n2 = ListNode(2) n1 = ListNode(1, n2) m2 = ListNode(2) m1 = ListNode(1, m2) self.assertEqual('1122', self.NodeListStr(Solution().mergeTwoLists(n1, m1))) self.assertEqual(None, Solution().mergeTwoLists(None, None))
def main(): # Build the linked lists with the test inputs # Start pointer references the head, which we pass to solution listOneStart = listOne = ListNode(2) listOne.next = ListNode(4) listOne = listOne.next listOne.next = ListNode(3) listOne = listOneStart listTwoStart = listTwo = ListNode(5) listTwo.next = ListNode(6) listTwo = listTwo.next listTwo.next = ListNode(4) listTwo = listTwoStart # Instantiate Solution class s = Solution() # Run the Solution method answer = s.addTwoNumbers(listOne, listTwo) # Loop through answer list and print all nodes while answer: print(str(answer.val)) answer = answer.next
def main(): # Build up both lists with the test values, node by node # testLists are pointers to the head, which we will pass in to the solution testList1 = buildList1 = ListNode(1) # 1, 2, 4 buildList1.next = ListNode(2) buildList1 = buildList1.next buildList1.next = ListNode(4) buildList1 = buildList1.next testList2 = buildList2 = ListNode(1) # 1, 3, 4 buildList2.next = ListNode(3) buildList2 = buildList2.next buildList2.next = ListNode(4) buildList2 = buildList2.next # Instantiate Solution class s = Solution() # Run the Solution method on all test cases answer = s.mergeTwoLists(testList1, testList2) # Traverse the list and print each node's value while answer: print(answer.val) answer = answer.next
def main(): # Create first list node and set the head pointer there testListHead = testList = ListNode(1) # Populate the remaining nodes of the test list for i in range(2, 6): testList.next = ListNode(i) testList = testList.next # Set the pointer back to the beginning of the linked list testList = testListHead # Instantiate Solution class s = Solution() # Run the solution answer = s.reverseList(testList) # Traverse the list and print each value one by one print("Final answer") while answer: print(answer.val) answer = answer.next
from Solution import Solution from Solution import ListNode from unittest import TestCase sol = Solution() tc = TestCase() l1 = ListNode(1, ListNode(2, ListNode(4))) l2 = ListNode(1, ListNode(3, ListNode(4))) tc.assertEqual(first=str(sol.mergeTwoLists(l1=l1, l2=l2)), second=str( ListNode( 1, ListNode( 1, ListNode(2, ListNode(3, ListNode(4, ListNode(4))))))), msg='Not Equal => Except value and Actual value') l1 = None l2 = None tc.assertEqual(first=str(sol.mergeTwoLists(l1=l1, l2=l2)), second=str(None), msg='Not Equal => Except value and Actual value') l1 = None
from Solution import Solution from Solution import ListNode from unittest import TestCase sol = Solution() tc = TestCase() head = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5))))) tc.assertEqual(first=str(sol.oddEvenList(head=head)), second=str( ListNode(1, ListNode(3, ListNode(5, ListNode(2, ListNode(4)))))), msg='Not Equal => Except value and Actual value') head = ListNode( 2, ListNode(1, ListNode(3, ListNode(5, ListNode(6, ListNode(4, ListNode(7))))))) tc.assertEqual(first=str(sol.oddEvenList(head=head)), second=str( ListNode( 2, ListNode( 3, ListNode( 6, ListNode(7, ListNode(1,
from Solution import Solution from Solution import ListNode from unittest import TestCase sol = Solution() tc = TestCase() head = ListNode(1, ListNode(2, ListNode(2, ListNode(1) ) ) ) tc.assertEqual( first=sol.isPalindrome(head=head), second=True, msg='Not Equal => Except value and Actual value' ) head = ListNode(1, ListNode(2) ) tc.assertEqual( first=sol.isPalindrome(head=head), second=False, msg='Not Equal => Except value and Actual value' )
from Solution import Solution from Solution import ListNode from unittest import TestCase sol = Solution() tc = TestCase() l1 = ListNode(2, ListNode(4, ListNode(3))) l2 = ListNode(5, ListNode(6, ListNode(4))) tc.assertEqual(first=str(sol.addTwoNumbers(l1=l1, l2=l2)), second=str(ListNode(7, ListNode(0, ListNode(8)))), msg='Not Equal => Except value and Actual value') l1 = ListNode(0) l2 = ListNode(0) tc.assertEqual(first=str(sol.addTwoNumbers(l1=l1, l2=l2)), second=str(ListNode(0)), msg='Not Equal => Except value and Actual value') l1 = ListNode( 9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9, ListNode(9))))))) l2 = ListNode(9, ListNode(9, ListNode(9, ListNode(9)))) tc.assertEqual(first=str(sol.addTwoNumbers(l1=l1, l2=l2)),
from Solution import ListNode from Solution import Solution l1 = ListNode(2) t1 = l1 t1.next = ListNode(4) t1 = t1.next t1.next = ListNode(3) l2 = ListNode(5) t2 = l2 t2.next = ListNode(6) t2 = t2.next t2.next = ListNode(4) sum = Solution() print(sum.addTwoNumbers(l1, l2))
from Solution import Solution from Solution import ListNode from unittest import TestCase sol = Solution() tc = TestCase() head = ListNode(1, ListNode(2)) tc.assertEqual(first=str(sol.reverseList(head=head)), second=str(ListNode(2, ListNode(1))), msg='Not Equal => Except value and Actual value') head = ListNode() tc.assertEqual(first=str(sol.reverseList(head=head)), second=str(ListNode()), msg='Not Equal => Except value and Actual value') print('ALL CLEAR')
from Solution import Solution from Solution import ListNode from unittest import TestCase sol = Solution() tc = TestCase() head = ListNode(1, ListNode(2, ListNode(3, ListNode(4)))) tc.assertEqual(first=str(sol.swapPairs(head=head)), second=str(ListNode(2, ListNode(1, ListNode(4, ListNode(3))))), msg='Not Equal => Except value and Actual value') head = ListNode() tc.assertEqual(first=str(sol.swapPairs(head=head)), second=str(ListNode()), msg='Not Equal => Except value and Actual value') head = ListNode(1) tc.assertEqual(first=str(sol.swapPairs(head=head)), second=str(ListNode(1)), msg='Not Equal => Except value and Actual value') print('ALL CLEAR')