题目:https://leetcode.com/problems/add-two-numbers/
该题比较简单,使用常规的解法即可。
解法:同时遍历两个链表,对每个链表的元素进行相加即可。
主要点:头节点+链表插入
// 伪代码
int sum, c;
sum = c = 0;
ListNode *head = new ListNode(0), *pos;
pos = head;
while(c!=0 || l1!=nullptr || l2!=nullptr)
sum = l1==nullptr?0:l1->data + l2==nullptr?0:l2->data + c;
c = sum / 10;
sum = sum % 10;
pos->next = new ListNode(sum);
pos = pos->next;
return head->next;