zoukankan      html  css  js  c++  java
  • 两个链表中的数相加

    示例:

    输入:(2 -> 4 -> 3)+ (5 -> 6 -> 4)

    输出: 7 -> 0 -> 8

    规则:342 + 465 = 807

    Python解决方案:

       # Definition for singly-linked list.
      # class ListNode(object):
      #   def __init__(self, x):
      #     self.val = x
      #     self.next = None

      def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ num1 = l1.val times = 1 while l1.next: num1 += l1.next.val*10**times l1 = l1.next times += 1 num2 = l2.val times = 1 while l2.next: num2 += l2.next.val*10**times l2 = l2.next times += 1 num = str(num1 + num2) head = ListNode(0) tmp = head for i in range(len(num)-1,-1,-1): tmp.next = ListNode(int(num[i])) tmp = tmp.next return head.next
  • 相关阅读:
    ZOJ 4097 Rescue the Princess
    最大值最小化 最小值最大化
    SD第九届省赛B题 Bullet
    Euler Circuit UVA
    bzoj 1878
    随笔
    BZOJ
    主席树模板
    AC自动机模板
    BZOJ
  • 原文地址:https://www.cnblogs.com/wenqinchao/p/10528878.html
Copyright © 2011-2022 走看看