LeetCode002 Add Two Numbers C语言

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

Subscribe to see which companies asked this question

题意:实际上就是342+465=807然后倒序输出。【一开始也没有看明白。】

参考别人的。一开始题意都没看明白。。。。。。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *l3 = (struct ListNode *)malloc(sizeof(struct ListNode));
    l3->val=0;
    l3->next=NULL;
    //这边因为l3是一直往后遍历的,所以返回头的话要加个head指示一下。
    struct ListNode* head = l3;
    l3->val=l1->val+l2->val;
    //while循环判断是否申请新的节点
    l1=l1->next;
    l2=l2->next;
    while(l1||l2||l3->val>9){
        l3->next=(struct ListNode *)malloc(sizeof(struct ListNode));
        l3->next->val=0;
        l3->next->next=NULL;
        if(l1){
            l3->next->val+=l1->val;
            l1=l1->next;
        }
        if(l2){
            l3->next->val+=l2->val;
            l2=l2->next;
        }
        if(l3->val>9){
            l3->next->val+=l3->val/10;
            l3->val=l3->val%10;
        }
        l3=l3->next;
    }
    return head;
}

PS:主要是搞清楚有几种情况。9+8这种需要申请节点;12+9这种不等长的;

注意C语言链表的使用。。。

时间: 2024-12-09 09:53:03

LeetCode002 Add Two Numbers C语言的相关文章

leetcode 5. 两个链表逐个元素相加 Add Two Numbers

7问题:Add Two Numbers 难度-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 single digit. Add the two numbers and return it as a linked list. Input: (2 -

我的LC之路-2. Add Two Numbers

写在前面:在LeetCode上做的题,代码基本都是我自己写的,效率有好有坏,仅供参考.有少数几道题苦思冥想做不出来的,可能会借鉴网上大神的解法,然后再自己编写,借鉴过他人解法的题会有说明.本人不是算法出身,语言基本靠自学,有任何错误理解或者不当举措,还请各位大侠手下留情并不吝赐教!万分感谢~ 依旧先把题搬一下: 2. Add Two Numbers You are given two non-empty linked lists representing two non-negative int

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