{"id":61,"date":"2025-11-16T07:00:54","date_gmt":"2025-11-15T23:00:54","guid":{"rendered":"https:\/\/154.36.158.208\/?p=61"},"modified":"2025-11-16T07:00:55","modified_gmt":"2025-11-15T23:00:55","slug":"%e4%b8%a4%e6%95%b0%e7%9b%b8%e5%8a%a0","status":"publish","type":"post","link":"https:\/\/www.xieyuchen.xyz\/index.php\/2025\/11\/16\/%e4%b8%a4%e6%95%b0%e7%9b%b8%e5%8a%a0\/","title":{"rendered":"\u4e24\u6570\u76f8\u52a0"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/154.36.158.208\/wp-content\/uploads\/2025\/11\/A13ABC117CCBF1A6AA5DC62FEE0A39CB.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  fetchpriority=\"high\" decoding=\"async\" width=\"869\" height=\"708\" data-original=\"http:\/\/154.36.158.208\/wp-content\/uploads\/2025\/11\/A13ABC117CCBF1A6AA5DC62FEE0A39CB.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-60\"  sizes=\"(max-width: 869px) 100vw, 869px\" \/><\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u7684\u5199\u6cd5\u662f\u8fd9\u6837<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8f93\u5165: l1=[2,4,3], l2=[5,6,4]<br>\u2193<br>\u53cd\u8f6c: [3,4,2], [4,6,5]<br>\u2193<br>\u8f6c\u6570\u5b57: 342, 465<br>\u2193<br>\u76f8\u52a0: 807<br>\u2193<br>\u8f6c\u94fe\u8868: [8,0,7]<br>\u2193<br>\u518d\u53cd\u8f6c: [7,0,8] <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Solution:\n    def addTwoNumbers(self, l1, l2):\n        def reverse_list(head):\n            prev = None\n            current = head\n            while current:\n                t = current.next\n                current.next = prev\n                prev = current\n                current = t\n            return prev\n        def link_to_number(head):\n            num = 0\n            current = head\n            while current:\n                num = num * 10 + current.val  # 123 = 1*100 + 2*10 + 3\n                current = current.next\n            return num\n        def number_to_link(num):\n            if num == 0:\n                return ListNode(0)\n            head = None\n            while num > 0:\n                g = num % 10\n                node = ListNode(g)\n                node.next = head\n                head = node\n                num \/\/= 10\n            return head\n        fanzhuan1 = reverse_list(l1) \n        fanzhuan2 = reverse_list(l2)\n        bianli1 = link_to_number(fanzhuan1)  \n        bianli2 = link_to_number(fanzhuan2)\n        he = bianli1 + bianli2\n        result = number_to_link(he)\n        final_result = reverse_list(result)\n        return final_result  \n\u6807\u51c6\u7b54\u6848\u4e0d\u7528\u7b2c\u4e00\u6b21\u53cd\u8f6c\u76f4\u63a5\u9006\u4f4d<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>class ListNode:\n    def __init__(self, val=0, next=None):\n        self.val = val\n        self.next = next\n\nclass Solution:\n    def addTwoNumbers(self, l1, l2):\n        \"\"\"\n        \u6807\u51c6\u89e3\u6cd5\uff1a\u9010\u4f4d\u76f8\u52a0\uff0c\u5904\u7406\u8fdb\u4f4d\n        \u65f6\u95f4\u590d\u6742\u5ea6: O(max(m, n))\uff0cm \u548c n \u5206\u522b\u662f\u4e24\u4e2a\u94fe\u8868\u7684\u957f\u5ea6\n        \u7a7a\u95f4\u590d\u6742\u5ea6: O(max(m, n))\uff0c\u65b0\u94fe\u8868\u7684\u957f\u5ea6\u6700\u591a\u4e3a max(m, n) + 1\n        \"\"\"\n        # \u521b\u5efa\u865a\u62df\u5934\u8282\u70b9\uff0c\u7b80\u5316\u4ee3\u7801\n        dummy = ListNode(0)\n        current = dummy\n        carry = 0  # \u8fdb\u4f4d\n        \n        # \u540c\u65f6\u904d\u5386\u4e24\u4e2a\u94fe\u8868\uff0c\u76f4\u5230\u90fd\u4e3a\u7a7a\u4e14\u6ca1\u6709\u8fdb\u4f4d\n        while l1 or l2 or carry:\n            # \u83b7\u53d6\u5f53\u524d\u4f4d\u7684\u503c\uff0c\u5982\u679c\u94fe\u8868\u4e3a\u7a7a\u5219\u4e3a 0\n            val1 = l1.val if l1 else 0\n            val2 = l2.val if l2 else 0\n            \n            # \u8ba1\u7b97\u5f53\u524d\u4f4d\u7684\u548c\uff08\u5305\u62ec\u8fdb\u4f4d\uff09\n            total = val1 + val2 + carry\n            \n            # \u8ba1\u7b97\u5f53\u524d\u4f4d\u7684\u503c\u548c\u65b0\u7684\u8fdb\u4f4d\n            carry = total \/\/ 10\n            digit = total % 10\n            \n            # \u521b\u5efa\u65b0\u8282\u70b9\u5e76\u8fde\u63a5\u5230\u7ed3\u679c\u94fe\u8868\n            current.next = ListNode(digit)\n            current = current.next\n            \n            # \u79fb\u52a8\u5230\u4e0b\u4e00\u4e2a\u8282\u70b9\n            l1 = l1.next if l1 else None\n            l2 = l2.next if l2 else None\n        \n        # \u8fd4\u56de\u7ed3\u679c\u94fe\u8868\u7684\u5934\u8282\u70b9\uff08\u8df3\u8fc7\u865a\u62df\u5934\u8282\u70b9\uff09\n        return dummy.next\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u6211\u7684\u5199\u6cd5\u662f\u8fd9\u6837 \u8f93\u5165: l1=[2,4,3], l2=[5,6,4]\u2193\u53cd\u8f6c: [3,4,2], [4,6,5] [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":34,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-61","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/comments?post=61"}],"version-history":[{"count":1,"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":62,"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/posts\/61\/revisions\/62"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/media\/34"}],"wp:attachment":[{"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/media?parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/categories?post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xieyuchen.xyz\/index.php\/wp-json\/wp\/v2\/tags?post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}