求两个链表相加;直接贴代码了,感觉这个题目没有说清楚。
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if (l1 == NULL) return l2;
if (l2 == NULL) return l1;
ListNode* head = nullptr;
ListNode* tmp_node1 = nullptr;
ListNode* tmp_node2 = nullptr;
ListNode* node1 = l1;
head = l1;
ListNode* node2 = l2;
int index_bit = 0;
int two_sum = 0;
int last_bit = 0;
tmp_node1 = node1;
while (node1 != NULL && node2 != NULL) {
index_bit = 0;
two_sum = node1->val + node2->val + last_bit;
if (two_sum >= 10) {
index_bit = 1;
last_bit = 1;
two_sum -= 10;
} else {
last_bit = 0;
}
std::cout << "two_sum: " << two_sum << std::endl;
node1->val = two_sum;
tmp_node1 = node1;
node1 = node1->next;
node2 = node2->next;
}
if (node1 == NULL && node2 == NULL && last_bit == 1) {
tmp_node2 = new ListNode(last_bit);
tmp_node1->next = tmp_node2;
tmp_node1 = tmp_node2;
return head;
}
ListNode* node = NULL;
if (node1 != NULL) {
node = node1;
}
if (node2 != NULL) {
node = node2;
}
tmp_node1->next = node;
tmp_node1 = node;
while (node != NULL) {
two_sum = node->val + last_bit;
if (two_sum >= 10) {
index_bit = 1;
last_bit = 1;
two_sum -= 10;
} else {
last_bit = 0;
}
node->val = two_sum;
tmp_node1 = node;
node = node->next;
}
if (last_bit != 0) {
tmp_node2 = new ListNode(last_bit);
tmp_node1->next = tmp_node2;
tmp_node1 = tmp_node2;
}
return head;
}
};
再然后贴一个其他附加代码。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
if (l1 == NULL) return l2;
if (l2 == NULL) return l1;
ListNode* head = nullptr;
ListNode* tmp_node1 = nullptr;
ListNode* tmp_node2 = nullptr;
ListNode* node1 = l1;
head = l1;
ListNode* node2 = l2;
int index_bit = 0;
int two_sum = 0;
int last_bit = 0;
tmp_node1 = node1;
while (node1 != NULL && node2 != NULL) {
index_bit = 0;
two_sum = node1->val + node2->val + last_bit;
if (two_sum >= 10) {
index_bit = 1;
last_bit = 1;
two_sum -= 10;
} else {
last_bit = 0;
}
std::cout << "two_sum: " << two_sum << std::endl;
node1->val = two_sum;
tmp_node1 = node1;
node1 = node1->next;
node2 = node2->next;
}
if (node1 == NULL && node2 == NULL && last_bit == 1) {
tmp_node2 = new ListNode(last_bit);
tmp_node1->next = tmp_node2;
tmp_node1 = tmp_node2;
return head;
}
ListNode* node = NULL;
if (node1 != NULL) {
node = node1;
}
if (node2 != NULL) {
node = node2;
}
tmp_node1->next = node;
tmp_node1 = node;
while (node != NULL) {
two_sum = node->val + last_bit;
if (two_sum >= 10) {
index_bit = 1;
last_bit = 1;
two_sum -= 10;
} else {
last_bit = 0;
}
node->val = two_sum;
tmp_node1 = node;
node = node->next;
}
if (last_bit != 0) {
tmp_node2 = new ListNode(last_bit);
tmp_node1->next = tmp_node2;
tmp_node1 = tmp_node2;
}
return head;
}
};
void CreateList(ListNode ** proot, int *arr, int len) {
ListNode* head = new ListNode(arr[0]);
ListNode* node1 = head;
ListNode* node2 = NULL;
int i = 1;
while (i < len) {
node2 = new ListNode(arr[i]);
node1->next = node2;
node1 = node2;
i++;
}
*proot = head;
}
void PrintList(ListNode *l) {
int i = 0;
ListNode * node = l;
while (node != NULL) {
std::cout << "index: " << i << " val: " << node->val << std::endl;
node = node->next;
i++;
}
std::cout << "----------------------" << std::endl;
}
// using namespace std;
int main(int argc, char *argv[]) {
int arr1[] = {2,4,3};
int arr2[] = {5,6,4};
ListNode* l1 = NULL;
ListNode* l2 = NULL;
CreateList(&l1, arr1, 3);
PrintList(l1);
CreateList(&l2, arr2, 3);
PrintList(l2);
Solution sol;
ListNode* l3 = sol.addTwoNumbers(l1, l2);
PrintList(l3);
return 0;
}