【LeetCode】链表 linked list(共34题)
/*--> */ /*--> */ 【2】Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序。 Example: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { 12 if (!l1 || !l2) { 13 return l1 == nullptr ? l2 : l1; 14 } 15 ListNode *h1 = l1, *h2 = l2; 16 ListNode *head = 0, *tail = 0; 17 int carry = 0; 18 while (h1 &&