{"action":"opened","issue":{"url":"https://api.github.com/repos/Huahua-Chen/program/issues/2","repository_url":"https://api.github.com/repos/Huahua-Chen/program","labels_url":"https://api.github.com/repos/Huahua-Chen/program/issues/2/labels{/name}","comments_url":"https://api.github.com/repos/Huahua-Chen/program/issues/2/comments","events_url":"https://api.github.com/repos/Huahua-Chen/program/issues/2/events","html_url":"https://github.com/Huahua-Chen/program/issues/2","id":249918326,"number":2,"title":"Add Two Numbers","user":{"login":"Huahua-Chen","id":24753024,"avatar_url":"https://avatars3.githubusercontent.com/u/24753024?v=4","gravatar_id":"","url":"https://api.github.com/users/Huahua-Chen","html_url":"https://github.com/Huahua-Chen","followers_url":"https://api.github.com/users/Huahua-Chen/followers","following_url":"https://api.github.com/users/Huahua-Chen/following{/other_user}","gists_url":"https://api.github.com/users/Huahua-Chen/gists{/gist_id}","starred_url":"https://api.github.com/users/Huahua-Chen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Huahua-Chen/subscriptions","organizations_url":"https://api.github.com/users/Huahua-Chen/orgs","repos_url":"https://api.github.com/users/Huahua-Chen/repos","events_url":"https://api.github.com/users/Huahua-Chen/events{/privacy}","received_events_url":"https://api.github.com/users/Huahua-Chen/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-08-14T02:28:11Z","updated_at":"2017-08-14T02:28:11Z","closed_at":null,"body":"### [问题](https://leetcode.com/problems/add-two-numbers/description/)\r\nYou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.\r\n\r\nYou may assume the two numbers do not contain any leading zero, except the number 0 itself.\r\n\r\nInput: (2 -> 4 -> 3) + (5 -> 6 -> 4)\r\nOutput: 7 -> 0 -> 8\r\n### 思路\r\n![addTwoNumber](https://github.com/Huahua-Chen/images/blob/master/images_inBlog/addTwoNumber.PNG?raw=true)\r\n注：上面ca>=10时为1；\r\n\r\n将两个链表的val相加，当sum值大于等于10时，保存余数到结果的相应节点，ca进位，循环到下一个节点重复操作。当两个链表的节点和ca都不存在或0是退出循环。\r\n**特殊情况**：两个链表不等长是，将较短的链表的节点值设为0；\r\n### 解决\r\n```js\r\n/**\r\n * Definition for singly-linked list.\r\n * function ListNode(val) {\r\n *     this.val = val;\r\n *     this.next = null;\r\n * }\r\n */\r\n/**\r\n * @param {ListNode} l1\r\n * @param {ListNode} l2\r\n * @return {ListNode}\r\n */\r\nvar addTwoNumbers = function(l1, l2) {\r\n    let result = new ListNode(0);\r\n    let current = result;\r\n    let ca = 0;\r\n\r\n    while (l1 || l2 || ca) {\r\n      let val1 = l1 != null ? l1.val : 0;\r\n      let val2 = l2 != null ? l2.val : 0;\r\n\r\n      let sum = val1 + val2 + ca;\r\n      ca = sum > 9 ? 1 : 0;\r\n\r\n      current.next = new ListNode(sum % 10);\r\n      current = current.next;\r\n      if (l1) l1 = l1.next\r\n      if (l2) l2 = l2.next\r\n    }\r\n    \r\n    return result.next\r\n};\r\n```"}}
{"actor":{"display_login":"Huahua-Chen"}}