Add Two Numbers问题

问题描述:

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

这个问题还是比较简单的,应该主要是在考察对链表的掌握程度吧。

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    int carry=0;
    struct ListNode *p1=l1,*p2=l2,*answer,*p,*tmp;
    answer=malloc(sizeof(struct ListNode));
    p=answer;
    while(p1!=NULL||p2!=NULL||carry){
        p->val=carry?(p1?p1->val:0)+(p2?p2->val:0)+1:(p1?p1->val:0)+(p2?p2->val:0);
        carry=0;
        if(p->val>9){
            p->val%=10;
            carry=1;
        }
        p1=p1?p1->next:NULL;
        p2=p2?p2->next:NULL;
        tmp=malloc(sizeof(struct ListNode));
        p->next=tmp;
        p=p->next;
    }
    p->next=NULL;
    for(p=answer;p->next->next!=NULL;p=p->next)
        ;
    tmp=p->next;
    p->next=NULL;
    free(tmp);
    return answer;
}

AC倒是AC了,仅仅击败了2.32%的C提交....果断不能忍啊。让我想想,malloc应该开销挺大的,与其创建一个崭新的链表,不如在l1或者l2的基础上修改原来的链表。

struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    int carry=0;
    struct ListNode *p1=l1,*p2=l2,*tmp,*p;
    while(p1->val!=10||p2!=NULL||carry){
        p1->val=carry?(p1->val!=10?p1->val:0)+(p2?p2->val:0)+1:(p1->val!=10?p1->val:0)+(p2?p2->val:0);
        carry=0;
        if(p1->val>9){
            p1->val%=10;
            carry=1;
        }
        p2=p2?p2->next:NULL;
        if(p1->next==NULL){
            p1->next=malloc(sizeof(struct ListNode));
            p1->next->val=10;
            p1->next->next=NULL;
        }
        p1=p1->next;
    }
    for(p=l1;p->next->next!=NULL;p=p->next)
        ;
    tmp=p->next;
    p->next=NULL;
    free(tmp);
    return l1;
}

时间从35ms减到了18ms,击败53%的C提交。如果在函数开始时先判断l1,l2哪一个长度更长,以更长的链表为基础链表的话应该是能获取更快的速度的,然后我试了一下,竟然还慢了1ms,应该是测试用例的问题吧。

不过这道题也没有多少追求速度的必要,做到这个程度我觉得已经可以收工了。

时间: 2024-10-16 14:03:58

Add Two Numbers问题的相关文章

2、Add Two Numbers

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:

LeetCode --- 2. Add Two Numbers

题目链接:Add Two Numbers 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

【LeetCode】Add Two Numbers

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 ->

Add Two Numbers(Linked List)

Add Two Numbers 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) + (

Add Two Numbers

题目: Add Two Numbers 题目描述: 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. 程序设计 链表节点定义 Def

Leetcode 02 Add Two Numbers

Add Two Numbers 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) + (

【Leet Code】Add Two Numbers

Add Two Numbers Total Accepted: 20255 Total Submissions: 88115My Submissions 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 numb

LeetCode: Add Two Numbers 题解

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 ->

leetcode速度才是王道 2. Add Two Numbers

2. Add Two Numbers 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)

leetcode 002 Add Two Numbers

Add Two Numbers My Submissions Question Total Accepted: 105768 Total Submissions: 494750 Difficulty: Medium 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