Exemplo n.º 1
0
def buildList(vals: List[int]) -> ListNode:
    head = ListNode()
    prev = head
    for v in vals:
        prev.next = ListNode(v)
        prev = prev.next
    return head.next
Exemplo n.º 2
0
def build_list(vals: List[int]) -> ListNode:
    dummy = ListNode()
    current = dummy
    for v in vals:
        current.next = ListNode(v)
        current = current.next
    return dummy.next
Exemplo n.º 3
0
 def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
     c = 0
     result = ListNode(-1)
     curr = result
     while l1 or l2 or c:
         v = c
         if l1:
             v += l1.val
             l1 = l1.next
         if l2:
             v += l2.val
             l2 = l2.next
         c = v // 10
         v = v % 10
         curr.next = ListNode(v)
         curr = curr.next
     return result.next
Exemplo n.º 4
0
def createList(a: List[int]) -> ListNode:
    head = None
    prev: ListNode = None
    for i in a:
        n = ListNode(i)
        if prev:
            prev.next = n
        else:
            head = n
        prev = n
    return head
Exemplo n.º 5
0
 def deleteDuplicates(self, head: ListNode) -> ListNode:
     current = head
     newHead = ListNode()
     newCurrent = newHead
     while current:
         next = current.next
         current0 = current
         while next and next.val == current.val:
             current = next
             next = next.next
         if current == current0:
             newCurrent.next = current
             newCurrent = newCurrent.next
             newCurrent.next = None
         current = next
     return newHead.next
Exemplo n.º 6
0
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        class ListEntry:
            def __init__(self, h: ListNode):
                self.h = h

            def __lt__(self, other):
                return self.h.val < other.h.val

        lists_heap = [ListEntry(le) for le in lists if le]
        heapq.heapify(lists_heap)
        head = ListNode()
        prev = head
        while lists_heap:
            list_entry = heapq.heappop(lists_heap)
            ln = list_entry.h.next
            prev.next = list_entry.h
            prev = prev.next
            if ln:
                list_entry.h = ln
                heapq.heappush(lists_heap, list_entry)
        return head.next
Exemplo n.º 7
0
            if current == current0:
                newCurrent.next = current
                newCurrent = newCurrent.next
                newCurrent.next = None
            current = next
        return newHead.next


def printList(l: ListNode):
    while l:
        print(l.val, flush=True, sep=' ', end=' ')
        l = l.next
    print()


l1 = ListNode(1)
l1.next = ListNode(2)
l1.next.next = ListNode(3)
l1.next.next.next = ListNode(3)
l1.next.next.next.next = ListNode(4)
l1.next.next.next.next.next = ListNode(4)
l1.next.next.next.next.next.next = ListNode(5)

printList(Solution().deleteDuplicates(l1))  # 1 2 5

l2 = ListNode(1)
l2.next = ListNode(1)
l2.next.next = ListNode(1)
l2.next.next.next = ListNode(2)
l2.next.next.next.next = ListNode(3)
Exemplo n.º 8
0
Memory Usage: 15.3 MB, less than 62.20% of Python3 online submissions for Reverse Linked List.
"""


class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        curr = head
        prev: ListNode = None
        while curr:
            next = curr.next
            curr.next = prev
            prev = curr
            curr = next
        return prev


def printList(l: ListNode):
    while l:
        print(l.val, flush=True, sep=' ', end=' ')
        l = l.next
    print()


l = ListNode(1)
l.next = ListNode(2)
l.next.next = ListNode(3)
l.next.next.next = ListNode(4)
l.next.next.next.next = ListNode(5)

printList(Solution().reverseList(l))  # 5 4 3 2 1
Exemplo n.º 9
0
            cnt = len1 - len2
            headA = advance(headA, cnt)
        if len2 > len1:
            cnt = len2 - len1
            headB = advance(headB, cnt)

        while headA and headB:
            if headA == headB:
                return headA
            headA = headA.next
            headB = headB.next

        return None


l1a = ListNode(4)
l1a.next = ListNode(1)
l1a.next.next = ListNode(8)
l1a.next.next.next = ListNode(4)
l1a.next.next.next.next = ListNode(5)
l1b = ListNode(5)
l1b.next = ListNode(6)
l1b.next.next = ListNode(1)
l1b.next.next.next = l1a.next.next

print(Solution().getIntersectionNode(l1a, l1b).val)  # 8

l2a = ListNode(1)
l2a.next = ListNode(9)
l2a.next.next = ListNode(1)
l2a.next.next.next = ListNode(2)
Exemplo n.º 10
0
def buildNumberAsList(arr: List[int]) -> ListNode:
    h = build_list(arr)

    if not h:
        return ListNode(0)
    return h
Exemplo n.º 11
0
            current.next = tail
            next.next = current

            if prev:
                prev.next = next

            prev = current
            current = tail
        return newHead


def printList(l: ListNode):
    while l:
        print(l.val, flush=True, sep=' ', end=' ')
        l = l.next
    print()


list1 = ListNode(1)
list1.next = ListNode(2)
list1.next.next = ListNode(3)
list1.next.next.next = ListNode(4)
list1.next.next.next.next = ListNode(5)
printList(Solution().swapPairs(list1))

list2 = ListNode(1)
list2.next = ListNode(2)
list2.next.next = ListNode(3)
list2.next.next.next = ListNode(4)
printList(Solution().swapPairs(list2))
Exemplo n.º 12
0
        curr: ListNode = head
        while curr:
            next = curr.next
            curr.next = prev
            prev = curr
            curr = next
        return prev

    def isPalindrome(self, head: ListNode) -> bool:
        if head is None:
            return True
        middle = self.getMiddle(head)
        middle = self.reverse(middle)
        while middle:
            if middle.val != head.val:
                return False
            middle = middle.next
            head = head.next
        return True


l1 = ListNode(1)
l1.next = ListNode(2)
print(Solution().isPalindrome(l1))  # False

l2 = ListNode(1)
l2.next = ListNode(2)
l2.next.next = ListNode(2)
l2.next.next.next = ListNode(1)
print(Solution().isPalindrome(l2))  # True
Exemplo n.º 13
0
        fast: ListNode = head.next
        if fast is None:
            return False

        while fast:
            if fast == slow:
                return True
            fast = fast.next
            if fast is None:
                return False
            fast = fast.next
            slow = slow.next
        return False


l1 = ListNode(3)
l1.next = ListNode(2)
l1.next.next = ListNode(0)
l1.next.next.next = ListNode(-4)
l1.next.next.next.next = l1.next

print(Solution().hasCycle(l1))  # True

l2 = ListNode(1)
l2.next = ListNode(2)
l2.next.next = l2

print(Solution().hasCycle(l2))  # True

l3 = ListNode(1)
Exemplo n.º 14
0
                curr.val = curr.next.val
            prev2 = prev
            prev = curr
            curr = curr.next
        if prev2:
            prev2.next = None


def printList(l: ListNode):
    while l:
        print(l.val, flush=True, sep=' ', end=' ')
        l = l.next
    print()


l1 = ListNode(4)
n5 = l1.next = ListNode(5)
l1.next.next = ListNode(1)
l1.next.next.next = ListNode(9)

Solution().deleteNode(n5)

printList(l1)  # 4 1 9

l2 = ListNode(4)
l2.next = ListNode(5)
n1 = l2.next.next = ListNode(1)
l2.next.next.next = ListNode(9)

Solution().deleteNode(n1)
Exemplo n.º 15
0
            read = read.next
        if prev:
            prev.next = None
        else:
            return None
        return head


def printList(l: ListNode):
    while l:
        print(l.val, flush=True, sep=' ', end=' ')
        l = l.next
    print()


l1 = ListNode(1)
l1.next = ListNode(2)
l1.next.next = ListNode(6)
l1.next.next.next = ListNode(3)
l1.next.next.next.next = ListNode(4)
l1.next.next.next.next.next = ListNode(5)
l1.next.next.next.next.next.next = ListNode(6)

l2 = Solution().removeElements(l1, 6)
printList(l2)  # 1->2->3->4->5

printList(Solution().removeElements(l2, 6))  # 1->2->3->4->5

printList(Solution().removeElements(None, 6))  # []

printList(Solution().removeElements(ListNode(1), 1))  # []