{"id":1769,"date":"2016-02-05T00:33:29","date_gmt":"2016-02-04T16:33:29","guid":{"rendered":"http:\/\/boweihe.me\/?p=1769"},"modified":"2016-02-05T00:33:29","modified_gmt":"2016-02-04T16:33:29","slug":"leetcode-2-add-two-numbers","status":"publish","type":"post","link":"https:\/\/dayandcarrot.space\/?p=1769","title":{"rendered":"LeetCode #2.Add Two Numbers"},"content":{"rendered":"<p><strong>\u94fe\u63a5\uff1a<\/strong><a href=\"https:\/\/leetcode.com\/problems\/add-two-numbers\/\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/leetcode.com\/problems\/add-two-numbers\/<\/a><br \/>\n<strong>\u601d\u8def\uff1a<\/strong><br \/>\n\u4e24\u6761\u94fe\u8868\uff0c\u4ece\u5176\u4e2d\u4e00\u6bb5\u5f00\u59cb\u4fee\u6539\uff0c\u76f4\u63a5\u5728\u539f\u6709\u8282\u70b9\u4e0a\u505a\u52a0\u6cd5\u3002\u78b0\u5230\u5176\u4e2d\u4e00\u6761\u7ed3\u675f\u7684\u65f6\u5019\uff0c\u5982\u679c\u53e6\u4e00\u6761\u8fd8\u5728\u7ee7\u7eed\uff0c\u5219\u628anext\u6307\u5411\u53e6\u4e00\u6761\u94fe\u8868\u5f80\u540e\u4e00\u4e2a\u8282\u70b9\uff08\u611f\u89c9\u50cf\u662f\u94c1\u8f68\u5e76\u8f68\u4e00\u6837\uff09\u3002<br \/>\n\u6ce8\u610f\u5982\u679c\u52a0\u8d77\u6765\u8fd8\u6709\u591a\uff0c\u8981\u65b0\u5efa\u4e00\u4e2a\u8282\u70b9\u5b58\u653e\u3002<br \/>\n<strong>\u4ee3\u7801:<\/strong><\/p>\n<pre class=\"lang:c++ decode:true\">class Solution {\npublic:\n\tListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {\n\t\tListNode* retNode = l1;\n\t\tListNode* prevNode = NULL;\n\t\tint res = 0;\n\t\twhile (l1 != NULL &amp;&amp; l2 != NULL) {\n\t\t\tl1-&gt;val += (l2-&gt;val + res);\n\t\t\tres = 0;\n\t\t\twhile (l1-&gt;val &gt;= 10) {\n\t\t\t\tl1-&gt;val -= 10;\n\t\t\t\tres += 1;\n\t\t\t}\n\t\t\tprevNode = l1;\n\t\t\tl1 = l1-&gt;next;\n\t\t\tl2 = l2-&gt;next;\n\t\t}\n\t\tif (res == 0 &amp;&amp; l1 == NULL &amp;&amp; l2 == NULL)\n\t\t\treturn retNode;\n\t\tListNode* currNode = l1;\n\t\tif (l1 == NULL) {\n\t\t    prevNode-&gt;next = l2;\n\t\t\tcurrNode = l2;\n\t\t}\n\t\twhile (currNode != NULL &amp;&amp; res != 0) {\n\t\t\tcurrNode-&gt;val += res;\n\t\t\tres = 0;\n\t\t\twhile (currNode-&gt;val &gt;= 10) {\n\t\t\t\tcurrNode-&gt;val -= 10;\n\t\t\t\tres += 1;\n\t\t\t}\n\t\t\tprevNode = currNode;\n\t\t\tcurrNode = currNode-&gt;next;\n\t\t}\n\t\tif (res &gt; 0) {\n\t\t\t\/\/Add Node\n\t\t\tListNode* endNode = new ListNode(res);\n\t\t\tprevNode-&gt;next = endNode;\n\t\t}\n\t\treturn retNode;\n\t}\n};<\/pre>\n<p><strong>\u7ed3\u679c\uff1a<\/strong><br \/>\n<a href=\"http:\/\/boweihe.me\/wp-content\/uploads\/2016\/02\/LeetCode002.jpg\" rel=\"attachment wp-att-1770\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1770\" src=\"http:\/\/boweihe.me\/wp-content\/uploads\/2016\/02\/LeetCode002.jpg\" alt=\"LeetCode002\" width=\"1444\" height=\"735\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u94fe\u63a5\uff1ahttps:\/\/leetcode.com\/problems\/add-two-numbers\/ \u601d\u8def\uff1a \u4e24\u6761\u94fe\u8868\uff0c\u4ece\u5176\u4e2d\u4e00\u6bb5\u5f00\u59cb\u4fee\u6539\uff0c\u76f4\u63a5\u5728\u539f\u6709\u8282\u70b9\u4e0a\u505a\u52a0\u6cd5\u3002\u78b0\u5230\u5176\u4e2d\u4e00\u6761\u7ed3\u675f\u7684\u65f6\u5019\uff0c\u5982\u679c\u53e6\u4e00\u6761\u8fd8\u5728\u7ee7\u7eed\uff0c\u5219\u628anext\u6307\u5411\u53e6\u4e00\u6761\u94fe\u8868\u5f80\u540e\u4e00\u4e2a\u8282\u70b9\uff08\u611f\u89c9\u50cf\u662f\u94c1\u8f68\u5e76\u8f68\u4e00\u6837\uff09\u3002 \u6ce8\u610f\u5982\u679c\u52a0\u8d77\u6765\u8fd8\u6709\u591a\uff0c\u8981\u65b0\u5efa\u4e00\u4e2a\u8282\u70b9\u5b58\u653e\u3002 \u4ee3\u7801: class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* retNode = l1; ListNode* prevNode = NULL; int res = 0; while (l1 != NULL &amp;&amp; l2 != NULL) { l1-&gt;val += (l2-&gt;val + res); res = 0; while (l1-&gt;val &gt;= 10) { l1-&gt;val -= 10; res += 1; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[66],"class_list":["post-1769","post","type-post","status-publish","format-standard","hentry","category-study","tag-leetcode-oj"],"_links":{"self":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1769","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1769"}],"version-history":[{"count":0,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=\/wp\/v2\/posts\/1769\/revisions"}],"wp:attachment":[{"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1769"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1769"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dayandcarrot.space\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1769"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}