标签:color value ext pre cti 基础 log cin class
好吧,其实我的代码很冗杂 噗 其实也就是链表的基础知识。
/** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers = function(l1, l2) { var res = null; var head = null; var q = l1; var p = l2; var cin = 0; while(q != null && p != null) { var value = q.val + p.val + cin; cin = 0; var temp = new ListNode(value%10); if(value >= 10) { cin = 1; } if(head == null){ res = temp; head = res; } else { res.next = temp; res = res.next; } q = q.next; p = p.next; } while(q != null){ var value = q.val + cin; cin = 0; var temp = new ListNode(value % 10); if(value >= 10) { cin = 1; } res.next = temp; res = res.next; q = q.next; } while(p != null) { var value = p.val + cin; cin = 0; var temp = new ListNode(value % 10); if(value >= 10) { cin = 1; } res.next = temp; res = res.next; p = p.next } if(cin != 0){ var temp = new ListNode(cin); res.next = temp; res = res.next; } return head; };
标签:color value ext pre cti 基础 log cin class
原文地址:http://www.cnblogs.com/rimochiko/p/7604375.html