Пример #1
0
 def test_65536(self):
     """
     Test that add_two_nums_ll can handle larger integers
     """
     ll1 = LinkedList([6, 5, 5, 3, 6])
     ll2 = LinkedList([4, 3])
     ans = LinkedList([6, 5, 5, 7, 9])
     result = add_two_nums_ll(ll1.root, ll2.root)
     self.assertEqual(create_arr_from_ll(result), ans.data)
Пример #2
0
 def test_5(self):
     """
     Test that add_two_nums can handle small integers
     """
     ll1 = LinkedList([5])
     ll2 = LinkedList([6])
     ans = LinkedList([1, 1])
     result = add_two_nums_ll(ll1.root, ll2.root)
     self.assertEqual(create_arr_from_ll(result), ans.data)
Пример #3
0
            val = val1 + val2 + c
            if val >= 10:
                val -= 10
                c = 1
            else:
                c = 0
            node = ListNode(val)
            cur.next = node
            cur = cur.next
            if l1:
                l1 = l1.next
            if l2:
                l2 = l2.next
        if c:
            node = ListNode(c)
            cur.next = node
        return dummy.next


s = Solution()

l1 = LinkedList([9, 9]).head
l2 = LinkedList([9]).head
l = s.addLists(l1, l2)
printList(l)

l1 = LinkedList([7, 1, 6]).head
l2 = LinkedList([5, 9, 3]).head
l = s.addLists(l1, l2)
printList(l)
Пример #4
0
    import os
    import imp
    return imp.load_source(name, os.path.join(os.path.dirname(__file__),
                                              fpath))


load_src("helper", "../helper.py")
from helper import ListNode, LinkedList, printList


class Solution:
    """
    @param head: a ListNode
    @return: a ListNode
    """
    def swapPairs(self, head):
        # write your code here
        dummy = ListNode(0, head)
        cur = dummy
        while cur and cur.next and cur.next.next:
            tmp = cur.next
            cur.next = cur.next.next
            tmp.next = cur.next.next
            cur.next.next = tmp
            cur = cur.next.next
        return dummy.next


s = Solution()
head = LinkedList([1, 2, 3, 4]).head
printList(s.swapPairs(head))
Пример #5
0
        # write your code here
        dummy = ListNode(0)
        dummy.next = head
        pre = insert_node = dummy
        while head:
            if head.val < x:
                if insert_node.next != head:
                    pre.next = head.next
                    node = insert_node.next
                    insert_node.next = head
                    head.next = node
                    head = pre.next
                    insert_node = insert_node.next
                else:
                    insert_node = head
                    pre = head
                    head = head.next
            else:
                pre = head
                head = head.next
        return dummy.next


s = Solution()
head = LinkedList([1, 4, 3, 2, 5, 2]).head
#head = LinkedList([3, 3, 1, 2, 4]).head
head = s.partition(head, 3)
printList(head)
#Input:  list = 1->4->3->2->5->2->null, x = 3
#Output: 1->2->2->4->3->5->null
Пример #6
0
# Complete the function provided in the editor below. It has
# one parameter: a pointer to a Node object named  that points
# to the head of a linked list. Your function must return a boolean
# denoting whether or not there is a cycle in the list. If there
# is a cycle, return true; otherwise, return false.
# Max list size is 100!!
from helper import LinkedList

# class Node(object):
#     def __init__(self, data=None, next_node=None):
#         self.data = data
#         self.next = next_node

def has_cycle(node):
    max_size = 100
    for i in range(max_size):
        node = node.next
        if not node:
            return False
    return True

ll = LinkedList()
# ll.add_multiple([1,2,3,4,5])
one = ll.add(1)
two = ll.add(2)
three = ll.add(3)
three.next = two

print(has_cycle(ll.head))
Пример #7
0
    @param l2: ListNode l2 is the head of the linked list
    @return: ListNode head of linked list
    """
    def mergeTwoLists(self, l1, l2):
        # write your code here
        dummy = ListNode(0)
        cur = dummy
        while l1 and l2:
            if l1.val < l2.val:
                cur.next = l1
                l1 = l1.next
            else:
                cur.next = l2
                l2 = l2.next
            cur = cur.next
        while l1:
            cur.next = l1
            l1 = l1.next
            cur = cur.next
        while l2:
            cur.next = l2
            l2 = l2.next
            cur = cur.next
        return dummy.next


s = Solution()
l1 = LinkedList([1, 3, 8, 11, 15]).head
l2 = LinkedList([2]).head
head = s.mergeTwoLists(l1, l2)
printList(head)
Пример #8
0
class Solution:
    """
    @param: head: The first node of linked list.
    @return: a tree node
    """
    def sortedListToBST(self, head):
        # write your code here
        if not head:
            return None
        if not head.next:
            return TreeNode(head.val)

        pre, slow, fast = None, head, head
        while fast and fast.next:
            pre = slow
            slow = slow.next
            fast = fast.next.next

        mid = slow
        root = TreeNode(mid.val)
        root.right = self.sortedListToBST(mid.next)
        pre.next = None
        root.left = self.sortedListToBST(head)
        return root


s = Solution()
head = LinkedList([1, 2, 3]).head
root = s.sortedListToBST(head)
root.prettyPrint()
    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""

class Solution:
    """
    @param head: The first node of linked list.
    @param n: An integer
    @return: The head of linked list.
    """

    def removeNthFromEnd(self, head, n):
        # write your code here
        left, right = head, head
        for i in range(n):
            right = right.next
        if not right:
            return head.next
        while right.next:
            left = left.next
            right = right.next
        left.next = left.next.next
        return head

s = Solution()
head = LinkedList([5, 4, 3, 2, 1]).head
head = s.removeNthFromEnd(head, 2)
printList(head)
head = s.removeNthFromEnd(head, 4)
printList(head)
    import imp
    return imp.load_source(name, os.path.join(os.path.dirname(__file__), fpath))


load_src("helper", "../helper.py")

from helper import LinkedList, ListNode

class Solution:
    """
    @param head: the head of linked list.
    @return: a middle node of the linked list
    """

    def middleNode(self, head):
        # write your code here
        if not head:
            return None
        slow, fast = head, head.next
        while fast and fast.next:
            slow = slow.next
            fast = fast.next.next
        return slow

for i in range(6):
    ll = LinkedList(range(i))
    ll.print()
    s = Solution()
    mid = s.middleNode(ll.head)
    print(mid.val) if mid else print(None)
Пример #11
0
        cur = tail = head
        while cur:
            length += 1
            tail = cur
            cur = cur.next
        k = k % length
        if k == 0:
            return head
        cur = head
        for i in range(length - k - 1):
            cur = cur.next
        new_head = cur.next
        cur.next = None
        tail.next = head
        return new_head


s = Solution()

head = LinkedList([1, 2, 3, 2, 1]).head
head = s.rotateRight(head, 1)
printList(head)

head = LinkedList([0, 1]).head
head = s.rotateRight(head, 100)
printList(head)

head = LinkedList([1, 2, 3, 4, 5]).head
head = s.rotateRight(head, 2)
printList(head)
Пример #12
0
                cur.next = right
                right = right.next
            cur = cur.next
        while left:
            cur.next = left
            left = left.next
            cur = cur.next
        while right:
            cur.next = right
            right = right.next
            cur = cur.next
        return dummy.next

    def get_mid(self, head):
        if not head or not head.next:
            return head
        slow, fast = head, head.next
        while fast and fast.next:
            slow = slow.next 
            fast = fast.next.next 
        return slow

s = Solution()
head = LinkedList([1, 3, 2]).head
head = s.sortList(head)
printList(head)

head = LinkedList([1, 7, 2, 6]).head
head = s.sortList(head)
printList(head)