Esempio n. 1
0
    def getIntersectionNode(self, headA, headB):
        if not headA or not headB:
            return
        arrayA = ListNode.toArray(headA)
        arrayB = ListNode.toArray(headB)
        lenA = len(arrayA)
        lenB = len(arrayB)
        if lenA >= lenB:
            big = arrayA
            small = arrayB
        else:
            big = arrayB
            small = arrayA
        div = abs(lenA - lenB)
        loop = min(len(arrayA), len(arrayB)) - 1
        index = loop

        for i in range(loop, -1, -1):
            if small[i] == big[i + div]:
                index -= 1
            else:
                break

        array = small[index + 1:len(small)]
        return ListNode.toList(array)
Esempio n. 2
0
def main():
    a = ListNode.toList([1, 2])
    b = ListNode.toList([1, 2])
    headA = ListNode.toList(['a1', 'a2', 'c1', 'c2', 'c3'])
    headB = ListNode.toList(['b1', 'b2', 'b3', 'c1', 'c2', 'c3'])
    sln = Solution()
    sln.getIntersectionNode(headB, headA).show()
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        carry = 0

        # create a head node which does not contains value
        root = n = ListNode(0)

        while l1 or l2 or carry:
            v1 = 0
            v2 = 0
            if l1:
                v1 = l1.val
                l1 = l1.next

            if l2:
                v2 = l2.val
                l2 = l2.next

            carry, val = divmod(v1 + v2 + carry, 10)

            n.next = ListNode(val)
            n = n.next
        return root.next
Esempio n. 4
0
 def removeNthFromEnd(self, head, n):
     nodes = ListNode.toArray(head)
     print(nodes)
     index = len(nodes) - n
     del nodes[index]
     if len(nodes) == 0:
         return
     return ListNode.toList(nodes)
Esempio n. 5
0
 def fastest(self, l1: ListNode, l2: ListNode) -> ListNode:
     tmp = 0
     res = ans = ListNode(None)
     while l1 or l2 or tmp:
         tmp += (l1.val if l1 else 0) + (l2.val if l2 else 0)
         res.next = ListNode(tmp % 10)
         tmp //= 10
         res = res.next
         l1 = l1.next if l1 else None
         l2 = l2.next if l2 else None
     return ans.next
Esempio n. 6
0
 def mergeKLists(self, lists):
     if len(lists) == 0:
         return
     arrays = []
     for _ in lists:
         if not _:
             arrays += ListNode.toArray(_)
     if len(arrays) == 0:
         return
     arrays.sort()
     return ListNode.toList(arrays)
    def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
        if not l1:
            return l2

        if not l2:
            return l1

        if l1.val < l2.val:
            l1.next = self.mergeTwoLists(l1.next, l2)
            return l1
        else:
            l2.next = self.mergeTwoLists(l1, l2.next)
            return l2
Esempio n. 8
0
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        dummy = ListNode(0)
        dummy.next = head
        first = dummy
        second = dummy

        for i in range(n + 1):
            first = first.next

        while first is not None:
            first = first.next
            second = second.next

        second.next = second.next.next

        return dummy.next
Esempio n. 9
0
 def reverseKGroup(self, head, k):
     if not head:
         return
     array = ListNode.toArray(head)
     if len(array) == 0:
         return
     r = []
     _len = len(array)
     for i in range(0, _len, k):
         if i + k > _len:
             r += array[i: _len]
             break
         tmp = array[i:k+i]
         tmp.reverse()
         r += tmp
     return ListNode.toList(r)
Esempio n. 10
0
 def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
     head = l1
     d, m = divmod(l1.val + l2.val, 10)
     head.val = m
     while all([l1.next, l2.next]):
         l1 = l1.next
         l2 = l2.next
         l1.val += d
         d, m = divmod(l1.val + l2.val, 10)
         l1.val = m
     l1.next = l1.next if l1.next else l2.next
     while l1.next and d:
         l1 = l1.next
         d, m = divmod(l1.val + d, 10)
         l1.val = m
     if d:
         l1.next = ListNode(val=d)
     return head
Esempio n. 11
0
def main():
    sln = Solution()
    node1 = ListNode(1)
    node2 = ListNode(2)
    node3 = ListNode(3)
    node1.next = node2
    node2.next = node3
    print(sln.reverseList(node1).show())
Esempio n. 12
0
 def toList(self, array):
     # 将数组转换为链表
     if len(array) == 0:
         return
     nodes = [ListNode(a) for a in array]
     cur = nodes[0]
     for i in range(1, len(nodes)):
         cur.next = nodes[i]
         cur = nodes[i]
     return nodes[0]
Esempio n. 13
0
def insertion_sort_list(head):
    cur = parent = ListNode(None)
    while head:
        while cur.next and cur.next.val < head.val:
            cur = cur.next

        cur.next, head.next, head = head, cur.next, head.next

        cur = parent
    return cur.next
 def middleNode(self, head: ListNode) -> ListNode:
     fast = slow = ListNode(None)
     fast.next = head
     slow.next = head
     while fast is not None:
         slow = slow.next
         fast = fast.next
         if fast is None:
             break
         fast = fast.next
     return slow
    def swapPairs(self, head: ListNode) -> ListNode:
        dummy = pre = ListNode(0)

        pre.next = head

        while pre.next and pre.next.next:
            current = pre.next
            current_next = pre.next.next

            pre.next, current.next, current_next.next = current_next, current_next.next, current
            pre = current
        return dummy.next
Esempio n. 16
0
 def reverseList(self, head: ListNode) -> ListNode:
     # """迭代"""
     # n = temp = None
     # while head:
     #     n,head = head,head.next
     #     n.next = temp
     #     temp = n
     # return n
     """递归"""
     if head is None or head.next is None:
         return head
     newhead = self.reverseList(head.next)
     head.next.next = head
     head.next = None
     return newhead
 def mergeKLists(self, lists):
     """
     :type lists: List[ListNode]
     :rtype: ListNode
     """
     nodes = []
     for l in lists:
         while l:
             nodes.append(l)
             l = l.next
     nodes = sorted(nodes, key=lambda x: x.val)
     dummy = head = ListNode(0)
     for node in nodes:
         head.next = node
         head = head.next
     return dummy.next
 def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
     result = t = ListNode(0, head)
     temp = head
     k_list = list()
     while temp:
         k_list.append(temp)
         temp = temp.next
         if len(k_list) == k:
             h_node = k_list[-1].next
             for i in range(1, k):
                 k_list[i].next = k_list[i - 1]
             k_list[0].next = h_node
             t.next = k_list[-1]
             t = k_list[0]
             k_list = list()
     return result.next
    def best(self, head: ListNode, n: int) -> ListNode:
        """
        双指针,前指针超前后指针n+1,删除后指针的next
        :param head:
        :param n:
        :return:
        """
        dummy = ListNode(0, head)
        first = head
        second = dummy
        for i in range(n):
            first = first.next

        while first:
            first = first.next
            second = second.next

        second.next = second.next.next
        return dummy.next
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        from queue import PriorityQueue
        head = dummy = ListNode(0)

        count = 0

        q = PriorityQueue()

        for i in range(len(lists)):
            temp = lists[i]
            while temp:
                q.put((temp.val, count, temp))
                temp = temp.next
                count += 1

        while not q.empty():
            dummy.next = q.get()[2]
            dummy = dummy.next

        return head.next
    def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
        cur = head
        count = 0

        while cur and count != k:
            cur = cur.next
            count += 1

        if count == k:
            cur = self.reverseKGroup(cur, k)

            while count > 0:
                tmp = head.next
                head.next = cur
                cur = head
                head = tmp
                count -= 1
            head = cur

        return head
    def mergeKLists(self, lists: List[ListNode]) -> ListNode:
        head = dummy = ListNode(0)

        while True:
            min_val = 2147483647
            min_index = None
            to_break = True
            for index in range(len(lists)):
                if lists[index]:
                    if lists[index].val < min_val:
                        min_index = index
                        min_val = lists[index].val
                    to_break = False

            if to_break:
                break

            dummy.next = lists[min_index]
            dummy = dummy.next

            lists[min_index] = lists[min_index].next
        return head.next
Esempio n. 23
0
def main():
    sln = Solution()
    node1 = ListNode(-129)
    node2 = ListNode(-129)
    node1.next = node2
    print(sln.isPalindrome(node1))
Esempio n. 24
0

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if not head or not head.next:
            return head

        prev = None
        current = head

        while current:
            next_node = current.next
            current.next = prev
            prev = current
            current = next_node

        return prev


l = ListNode(0)
l.next = ListNode(1)
l.next.next = ListNode(2)

solution = Solution()
new_l = solution.reverseList(l)

assert new_l.val == 2
assert new_l.next.val == 1
assert new_l.next.next.val == 0

Esempio n. 25
0
def main():
    sln = Solution()
    array = [1, 2, 3, 4, 5]
    node = ListNode(1)
    head = node.toList(array)
    print(sln.reverseBetween(head, 2, 4).show())
Esempio n. 26
0
def main():
    head = ListNode(1)
    node1 = ListNode(2)
    node2 = ListNode(3)
    node3 = ListNode(4)
    node4 = ListNode(5)
    node5 = ListNode(6)
    node6 = ListNode(7)
    # head.next = node1
    node1.next = node2
    node2.next = node3
    node3.next = node4
    node4.next = node5
    node5.next = node6
    sln = Solution()
    print(sln.removeNthFromEnd(head, 1).show())
Esempio n. 27
0
def main():
    array = [1, 2]
    head = ListNode.toList(array)
    sln = Solution()
    print(sln.reverseKGroup(head, 2).show())