本题主要考察的是对链表的操作。在解题之前,先复习一下链表的头插法和尾插法的操作
不管采用哪种方法,首先应创建表头,目的是使第一个实际节点和后面的节点是等同的,不会因为删除、插入等操作区分开考虑。
头插法:不断的将新节点插入到表头后面。在头结点(为了操作方便,在单链表的第一个结点之前附加一个结点,称为头结点。头结点的数据域可以存储数据标题、表长等信息,也可以不存储任何信息,其指针域存储第一个结点的首地址)H之后插入数据,其特点是读入的数据顺序与线性表的逻辑顺序正好相反
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
则头插法的过程为:
ListNode *head, *p;
head->next = NULL;
for(int i = 0; i < N; i++)
{
p->next = head->next;
head->next = p;
}
尾插法:将每次插入的新结点放在链表的尾部
则尾插法的过程为:
ListNode *head, *r, *s;
head->next = NULL;
r = head;
for(int i = 0; i < N; i++)
{
s->next = r->next;
r->next = s;
r = s;
}
再来看本题,参考别人的博客分别使用头插法和尾插法解决
解法1:
#include<iostream>
#include<list>
using namespace std;
struct ListNode{
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL){}
};
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
{
ListNode retHead(-1), *pr = &retHead, *p1 = NULL, *p2 = NULL;
int carry = 0;
for(p1 = l1, p2 = l2; p1 != NULL && p2 !=NULL; p1 = p1->next, p2 = p2->next)
{
int sum = p1->val + p2->val + carry;
pr->next = new ListNode(sum % 10);
pr = pr->next;
carry = sum / 10;
}
for(ListNode *p = (p1 == NULL ? p2 : p1); p != NULL; p = p->next)
{
int sum = p->val + carry;
pr->next = new ListNode(sum % 10);
pr = pr->next;
carry = sum / 10;
}
if(carry != 0)
{
pr->next = new ListNode(carry);
}
return retHead.next;
}
int main()
{
ListNode* a1 = new ListNode(9);
ListNode* a2 = new ListNode(9);
a1->next = a2;
ListNode* b1 = new ListNode(1);
ListNode* result = addTwoNumbers(a1, b1);
while(result != NULL){
cout << result->val << " ";
result = result->next;
}
return 0;
}
解法2:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *head = (ListNode *)malloc(sizeof(ListNode));
ListNode *pre = head;
ListNode *node = NULL;
int c = 0,sum;
while(l1 != NULL && l2 != NULL){
sum = l1->val + l2->val + c;
c = sum / 10;
node = (ListNode *)malloc(sizeof(ListNode));
node->val = sum % 10;
node->next = NULL;
//尾插法
pre->next = node;
pre = node;
l1 = l1->next;
l2 = l2->next;
}
//例如:2->4->3->1 5->6->4
while(l1 != NULL){
sum = l1->val + c;
c = sum / 10;
node = (ListNode *)malloc(sizeof(ListNode));
node->val = sum % 10;
node->next = NULL;
//尾插法
pre->next = node;
pre = node;
l1 = l1->next;
}
//例如:2->4->3 5->6->4->1
while(l2 != NULL){
sum = l2->val + c;
c = sum / 10;
node = (ListNode *)malloc(sizeof(ListNode));
node->val = sum % 10;
node->next = NULL;
//尾插法
pre->next = node;
pre = node;
l2 = l2->next;
}
//最后一位还有进位
if(c > 0){
node = (ListNode *)malloc(sizeof(ListNode));
node->val = c;
node->next = NULL;
//尾插法
pre->next = node;
pre = node;
}
return head->next;
}
};
int main() {
Solution solution;
int A[] = {2,4,7,9};
int B[] = {5,6,4};
ListNode *head = NULL;
ListNode *head1 = (ListNode*)malloc(sizeof(ListNode));
ListNode *head2 = (ListNode*)malloc(sizeof(ListNode));
head1->next = NULL;
head2->next = NULL;
ListNode *node;
ListNode *pre = head1;
for(int i = 0;i < 4;i++){
node = (ListNode*)malloc(sizeof(ListNode));
node->val = A[i];
node->next = NULL;
pre->next = node;
pre = node;
}
pre = head2;
for(int i = 0;i < 3;i++){
node = (ListNode*)malloc(sizeof(ListNode));
node->val = B[i];
node->next = NULL;
pre->next = node;
pre = node;
}
head = solution.addTwoNumbers(head1->next,head2->next);
while(head != NULL){
printf("%d ",head->val);
head = head->next;
}
return 0;
}