LeetCode-2 Add Two Numbers

来源:互联网 发布:时序数据库应用场景 编辑:程序博客网 时间:2024/06/05 15:27

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

本题其实就是将两个链表的值对应加在一起产生一条和链表,但是要注意加和过程中进位的问题以及两条初始链表不一样长的问题。

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 *p = l1,*q = l2,*head,*r,*l;        head = (ListNode *)malloc(sizeof(ListNode));        head->next = NULL;        l = head;        int n = 0;        int temp = 0;        while(p!=NULL && q!=NULL)        {            int value;            value = p->val + q->val;            r = (ListNode *)malloc(sizeof(ListNode));            if(n==0)                head->val = value % 10;            else            {                r->val = (value + temp) % 10;                r->next = NULL;                l->next = r;                l=r;            }            if(n==0)                temp = (p->val + q->val) / 10;            else                temp = (value + temp) / 10;            n++;            p = p->next;            q = q->next;        }                ListNode *v;        if(p==NULL && q==NULL){            if(temp > 0){                r = (ListNode *)malloc(sizeof(ListNode));                r->val = temp;                r->next = NULL;                l->next = r;                temp = 0;            }        }        else if(p!=NULL){            ListNode *newp = p;            while(newp!=NULL){                int t = newp->val;                newp->val = (newp->val + temp) % 10;                temp = (t+temp) / 10;                v = newp;                newp = newp->next;            }            l->next = p;        }        else{            ListNode *newq = q;            while(newq!=NULL){                int t = newq->val;                newq->val = (newq->val + temp) % 10;                temp = (t+temp) / 10;                v = newq;                newq = newq->next;            }            l->next = q;        }        if(temp > 0){                ListNode *t = (ListNode *)malloc(sizeof(ListNode));                t->val = temp;                t->next = NULL;                v->next = t;            }        return head;    }};

写完之后看答案发现走了许多弯路..比如关于判断链表是否结束的while语句,没考虑周全就直接写了。

下面这个是Java版的简洁答案:

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {    ListNode dummyHead = new ListNode(0);    ListNode p = l1, q = l2, curr = dummyHead;    int carry = 0;    while (p != null || q != null) {        int x = (p != null) ? p.val : 0;        int y = (q != null) ? q.val : 0;        int sum = carry + x + y;        carry = sum / 10;        curr.next = new ListNode(sum % 10);        curr = curr.next;        if (p != null) p = p.next;        if (q != null) q = q.next;    }    if (carry > 0) {        curr.next = new ListNode(carry);    }    return dummyHead.next;}



0 0
原创粉丝点击