Esempio n. 1
0
def list2link(li):
    n = len(li)
    ans = ListNode(-1)
    cur = ans
    for i in range(n):
        cur.next = ListNode(li[i])
        cur = cur.next
    return ans.next
Esempio n. 2
0
    def Mundane(self, l1, l2):

        if not l1:
            return l2
        if not l2:
            return l1
        val = l1.val + l2.val
        ans = ListNode(val % 10)
        ans.next = self.addTwoNum(l1.next, l2.next)
        if val >= 10:
            ans.next = self.addTwoNum(ListNode(1), ans.next)

        return ans
Esempio n. 3
0
    def Mundane(self, head):

        ans = ListNode(-1)
        ans.next = head
        pre = ans
        cur = ans.next
        while cur and cur.next:
            tmp = cur.next
            pre.next = tmp
            cur.next = tmp.next
            tmp.next = cur
            pre = cur
            cur = cur.next
        return ans.next
Esempio n. 4
0
 def swapPairs(self, head: ListNode) -> ListNode:
     if not head or not head.next:
         return head
     cur = head.next
     head.next = self.swapPairs(cur.next)
     cur.next = head
     return cur
Esempio n. 5
0
 def mergeKLists(self, lists) -> ListNode:
     import heapq
     dummy = ListNode(0)
     p = dummy
     head = []
     for i in range(len(lists)):
         if lists[i]:
             heapq.heappush(head, (lists[i].val, i))
             lists[i] = lists[i].next
     while head:
         val, idx = heapq.heappop(head)
         p.next = ListNode(val)
         p = p.next
         if lists[idx]:
             heapq.heappush(head, (lists[idx].val, idx))
             lists[idx] = lists[idx].next
     return dummy.next
Esempio n. 6
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        s1 = ""
        s2 = ""
        p = l1
        q = l2
        while p:
            s1 += str(p.val)
            p = p.next
        while q:
            s2 += str(q.val)
            q = q.next
        res = str(int(s1) + int(s2))

        ans = ListNode(-1)
        pre = ans
        for i in res:
            pre.next = ListNode(i)
            pre = pre.next
        return ans.next
Esempio n. 7
0
 def addTwoNum(self, l1, l2):
     ans = ListNode(-1)
     cur = ans
     #表示进位
     flag = 0
     while l1 or l2:
         x = l1.val if l1 else 0
         y = l2.val if l2 else 0
         s = flag + x + y
         flag = s // 10
         #余数
         cur.next = ListNode(s % 10)
         cur = cur.next
         if l1:
             l1 = l1.next
         if l2:
             l2 = l2.next
     if flag > 0:
         cur.next = ListNode(1)
     return ans.next
Esempio n. 8
0
    def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
        stack1 = []
        stack2 = []

        while l1:
            stack1.append(l1.val)
            l1 = l1.next
        while l2:
            stack2.append(l2.val)
            l2 = l2.next

        node = None
        carry = 0

        while stack1 or stack2:
            x = stack1.pop() if stack1 else 0
            y = stack2.pop() if stack2 else 0

            val = x + y + carry
            carry = val // 10
            n = val % 10
            cur = ListNode(n)
            cur.next = node  #!!!!!!!!!!!!!!!!!!尾插法
            node = cur
        # 判断最高位是否有进位
        if carry != 0:
            res = ListNode(carry)
            res.next = cur
            return res
        return cur
Esempio n. 9
0
 def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
     ans = ListNode(-1)
     cur = ans
     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
     if l1:
         cur.next = l1
     if l2:
         cur.next = l2
     return ans.next