标签:
1、Add Two Numbers——这是leedcode的第二题:
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
给定两个list,list里面的数字是按照逆序存储的,并且每一位包含一个数字。将两个list相加,返回一个list作为结果。这两个list相加的逻辑如下:各个位相加,逢十进一。
解决思路:
1、首先这是两个链表的相加问题。因为这里没有说链表谁长谁短,所以将链表先对应的位置相加赋值给结果链表,然后长的那个链表直接赋值给结果链表。
2、由于每个位置的数字只能是0-9的数字,所以会产生进位。所以链表在相加的时候,需要加上从前面得到的进位数。
3、如果最后加完所有的链表位置,还有进位制,这时候这个进位单独产生一个结点。
ps:其实就是两个数的加法运算,例子中的输入(逆序的)其实就是342+465的计算。
我们在计算的步骤分解:
2+5=7;
4+6=10;进一,得到0
2+5+1(进位)=8;
所以得到 807,逆序后就是708。
这里需要注意边界情况,例如:输入是[1]和[9,9]。
这里贴出Accept的代码:
Java版本
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode l3 = new ListNode(0);
ListNode result = new ListNode(-1);
int carry = 0;
while (l1 != null && l2 != null) {
int val = l1.val + l2.val + carry;
carry = 0;
if (val >= 10) {
carry = 1;
}
ListNode node = new ListNode(val % 10);
if (result.val == -1) {
result.val = 0;
result.next = node;
}
l3.next = node;
l3 = node;
l1 = l1.next;
l2 = l2.next;
}
while (l1 != null) {
int val = l1.val + carry;
carry = 0;
if (val >= 10) {
carry = 1;
}
ListNode node = new ListNode(val % 10);
l3.next = node;
l3 = node;
l1 = l1.next;
}
while (l2 != null) {
int val = l2.val + carry;
carry = 0;
if (val >= 10) {
carry = 1;
}
ListNode node = new ListNode(val % 10);
l3.next = node;
l3 = node;
l2 = l2.next;
}
if (carry > 0) {
ListNode node = new ListNode(carry % 10);
l3.next = node;
l3 = node;
}
result = result.next;
return result;
}
}
C++版本:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* start=NULL;
ListNode* l3 = NULL;
int carry = 0;
while (l1 != NULL&&l2 != NULL)
{
int value = l1->val + l2->val + carry;
carry = 0;
if (value >= 10) {
carry = 1;
}
ListNode* node = new ListNode(value % 10);
if (start == NULL&&l3 == NULL) {
start = node;
l3 = node;
}
else
{
l3->next = node;
l3 = l3->next;
}
l1 = l1->next;
l2 = l2->next;
}
while (l1 != NULL) {
int value = l1->val + carry;
carry = 0;
if (value >= 10) {
carry = 1;
}
ListNode* node = new ListNode(value % 10);
if (start == NULL&&l3 == NULL) {
start = node;
l3 = node;
}
else
{
l3->next = node;
l3 = l3->next;
}
l1 = l1->next;
}
while (l2 != NULL) {
int value = l2->val + carry;
carry = 0;
if (value >= 10) {
carry = 1;
}
ListNode* node = new ListNode(value % 10);
if (start == NULL&&l3 == NULL) {
start = node;
l3 = node;
}
else
{
l3->next = node;
l3 = l3->next;
}
l2 = l2->next;
}
if (carry > 0)
{
ListNode* node = new ListNode(carry % 10);
carry = 0;
if (start == NULL&&l3 == NULL) {
start = node;
l3 = node;
}
else
{
l3->next = node;
l3 = l3->next;
}
}
return start;
}
};
标签:
原文地址:http://blog.csdn.net/buptzhengchaojie/article/details/51347930