LeetCode#2 Add Two Numbers

来源:互联网 发布:linux 文件夹映射 编辑:程序博客网 时间:2024/06/07 00:50

题目:You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

       题意:两个单链表表示的数字相加,再将结果用单链表表示出来,考察链表的基本操作,注意进位即可。

       哎,好久没有敲过链表了,真的生疏了,题目给定的两个链表指针l1、l2常理来说指向第一个有效元素,不是指向头结点的指针。因此,返回的指针也是指向有效结点的指针,不是指向头结点。

       还有:链表最好使用带头结点的表示方法,方便链表操作!!头结点的数据域无用,随便初始化,用的是其指针域。

我写的代码想得比较复杂,运用也不够灵活

/** * 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 *head=(ListNode *)malloc(sizeof(ListNode));// ListNode* head=new ListNode(0);    head->val=0;    head->next=NULL;    ListNode *p1,*p2;    p1=head;    int vis=0;    while(l1!=NULL&&l2!=NULL)    {        p2=(ListNode *)malloc(sizeof(ListNode));        p2->val=(l1->val+l2->val+vis)%10;        p2->next=NULL;        p1->next=p2;        p1=p1->next;        if(l1->val+l2->val+vis>=10)vis=1;        else vis=0;        l1=l1->next;        l2=l2->next;    }    if(l1!=NULL)    {        while(l1!=NULL)        {            p2=(ListNode *)malloc(sizeof(ListNode));            p2->val=(l1->val+vis)%10;            p2->next=NULL;            p1->next=p2;            p1=p1->next;            if(vis+l1->val>=10)vis=1;            else vis=0;            l1=l1->next;        }    }    else if(l2!=NULL)    {        while(l2!=NULL)        {            p2=(ListNode *)malloc(sizeof(ListNode));            p2->val=(l2->val+vis)%10;            p2->next=NULL;            p1->next=p2;            p1=p1->next;            if(vis+l2->val>=10)vis=1;            else vis=0;            l2=l2->next;        }    }    if(vis==1){        p2=(ListNode *)malloc(sizeof(ListNode));        p2->val=1;        p2->next=NULL;        p1->next=p2;    }    return head->next;    }};

但是看了别人的代码,就觉得别人的代码思路清晰并且十分简洁,真是一目了然,所以说编程真是一门艺术

/** * 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) {        int flag = 0;        ListNode* tail = new ListNode(0);        ListNode* ptr = tail;        while(l1 != NULL || l2 != NULL){            int val1 = 0;            if(l1 != NULL){                val1 = l1->val;                l1 = l1->next;            }            int val2 = 0;            if(l2 != NULL){                val2 = l2->val;                l2 = l2->next;            }            int tmp = val1 + val2 + flag;            ptr->next = new ListNode(tmp % 10);            flag = tmp / 10;            ptr = ptr->next;        }        if(flag == 1){            ptr->next = new ListNode(1);        }        return tail->next;    }};
原创粉丝点击