Esempio n. 1
0
            while h:
                # get the two merge head `h1`, `h2`
                h1, i = h, intv
                while i and h:
                    h, i = h.next, i - 1
                if i: break  # no need to merge because the `h2` is None.
                h2, i = h, intv
                while i and h:
                    h, i = h.next, i - 1
                c1, c2 = intv, intv - i  # the `c2`: length of `h2` can be small than the `intv`.
                # merge the `h1` and `h2`.
                while c1 and c2:
                    if h1.val < h2.val:
                        pre.next, h1, c1 = h1, h1.next, c1 - 1
                    else:
                        pre.next, h2, c2 = h2, h2.next, c2 - 1
                    pre = pre.next
                pre.next = h1 if c1 else h2
                while c1 > 0 or c2 > 0:
                    pre, c1, c2 = pre.next, c1 - 1, c2 - 1
                pre.next = h
            intv *= 2
        return res.next


if __name__ == '__main__':

    head = [5, 3, 4, 0, 2]
    head = generator_link(head)
    travel(Solution1().sortList(head))
                l1 = l1.next
            else:
                re.next = l2
                l2 = l2.next
            re = re.next
        if  l1:
            re.next = l1
        if  l2:
            re.next = l2

        return head.next


    
list1 = [1,2,4]

list2 = [1,3,4]

l1 = generator_link(list1)
l2 = generator_link(list2)

re = Solution().mergeTwoLists(l1,l2)


travel(re)





Esempio n. 3
0
                            pre_B = pre_B.next
                            pre_A = pre_A.next
                    if not pre_A and not pre_B:
                        return x
                    continue
                else:
                    pre_B = pre_B.next
            pre_B = re
            pre_A = pre_A.next
        return x

    '''答案解法'''

    def getintersectionNode(self, headA: ListNode,
                            headB: ListNode) -> ListNode:
        if not headA or not headB:
            return None
        nodeA = headA
        nodeB = headB
        while (nodeA != nodeB):
            nodeA = nodeA.next if nodeA else headB
            nodeB = nodeB.next if nodeB else headA
        return nodeA


listA = [4, 1, 8, 4, 5]
listB = [5, 0, 1, 8, 4, 5]
linkA = generator_link(listA)
linkB = generator_link(listB)
travel(Solution().getintersectionNode(linkA, linkB))
Esempio n. 4
0
from linked_list.leetcode import generator_link, travel


class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if not head:
            return head
        dummpy = ListNode(0, head)
        cur = dummpy
        while cur.next and cur.next.next:
            if cur.next.val == cur.next.next.val:
                x = cur.next.val
                while cur.next and cur.next.val == x:
                    cur.next = cur.next.next
            else:
                cur = cur.next
        return dummpy.next


list_node = [1, 2, 2, 2]

node1 = generator_link(list_node)

travel(Solution().deleteDuplicates(node1))
Esempio n. 5
0
class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        if not head:
            return
        if not head.next:
            return head
        p = head
        pre = None
        post = p.next
        while post:
            temp = post.next
            post.next = p
            p.next = pre
            pre = p
            p = post
            post = temp
        return p


    def reverselist(self,head:ListNode):
        if  head.next == None : return head
        newnode = self.reverselist(head.next)
        head.next.next = head
        head.next = None
        return newnode

head = [1,2,3,4,5]
travel(Solution().reverselist(generator_link(head)))

Esempio n. 6
0
输入:head = [1,1,2]
输出:[1,2]

输入:head = [1,1,2,3,3]
输出:[1,2,3]
'''
from linked_list.leetcode import generator_link,travel
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def deleteDuplicates(self, head: ListNode) -> ListNode:
        if not head:
            return
        p = head
        while p and p.next:
            if p.val ==p.next.val:
                x = p.val
                while p.next and p.next.val==x:
                    p.next = p.next.next
            else:
                p = p.next
        return head
#思路
a = [1,1,2,3,3]
link_list = generator_link(a)
travel(Solution().deleteDuplicates(link_list))
from linked_list.leetcode import generator_link,travel
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution:
    def removeElements(self, head: ListNode, val: int) -> ListNode:
        if not head:
            return
        pre = ListNode(0)
        res = pre
        pre.next = head
        p = head
        while p:
            if p.val == val:
                pre.next = p.next
                p = p.next
            else:
                p = p.next
                pre = pre.next


        return res.next

list = [7,7,7,7]
val = 7
travel(Solution().removeElements(generator_link(list),val))