链式A+B

来源:互联网 发布:我的世界村民交易js 编辑:程序博客网 时间:2024/06/05 05:18

</pre>题目描述</h2><div class="subject-question" style="font-size:14px; line-height:1.6; margin-bottom:20px; color:rgb(51,51,51); font-family:arial,STHeiti,"Microsoft YaHei",宋体"><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.4em">有两个用链表表示的整数,每个结点包含一个数位。这些数位是反向存放的,也就是个位排在链表的首部。编写函数对这两个整数求和,并用链表形式返回结果。</p><p style="margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; line-height:1.4em">给定两个链表ListNode* <span style="color:rgb(0,0,0)">A</span>,ListNode* <span style="color:rgb(0,0,0)">B</span>,请返回A+B的结果(ListNode*)。</p><div class="“subject-test-case-sample”">测试样例:</div><div class="“subject-test-case-sample”"><pre style="margin-top:0.8em; margin-bottom:0.8em; padding:10px 0px 10px 10px; font-size:14px; position:relative; border-left:2px solid rgb(26,188,156); background:rgb(254,254,254); word-break:break-all; font-family:Courier,"Courier New",monospace; white-space:pre-wrap; word-wrap:break-word; color:rgb(51,51,51)">{1,2,3},{3,2,1}
返回:{4,4,4}
------------------------------------------------------
思路:从个位加起,关键点在于进位,另外不new新节点的话,要注意两数位数不同时的指针连接。
。。。。。
/*struct ListNode {    int val;    struct ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};*/class Plus {public:    ListNode* plusAB(ListNode* a, ListNode* b) {        // write code here        if (!a)            return b;        if (!b)            return a;                 ListNode* c = a;        int add = 0;        int carry = 0;//jin wei        ListNode* apre = nullptr;        ListNode* bpre = nullptr;        while (a && b){            add = carry + a->val + b->val;            if (add >= 10){                add %= 10;                carry = 1;            }else {                carry = 0;            }            a->val = add;            if (!a->next && !b->next && carry){                ListNode* node = new ListNode(1);                a->next = node;                return c;            }else {                apre = a;                a = a->next;                bpre = b;                b = b->next;            }                     }        if (!a)            a = b;                 while (a){            add = carry + a->val;            if (add >= 10){                add %= 10;                carry = 1;            }else {                carry = 0;            }            a->val = add;            apre->next = a;            apre = a;            if (a->next)                a = a->next;            else if (carry){                ListNode* node = new ListNode(1);                a->next = node;                a = nullptr;            }else                a = nullptr;        }                 return c;    }};

快慢指针和栈实现:
/*struct ListNode {    int val;    struct ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};*/class Palindrome {public:    bool isPalindrome(ListNode* pHead) {        // 快慢指针和栈stack        ListNode* fast = pHead;        ListNode* slow = pHead;        stack<int> s;        while (fast && fast->next){            s.push(slow->val);            fast = fast->next->next;            slow = slow->next;        }        if (fast)            //元素为奇数            slow = slow->next;//跳过中间节点        while (slow){            if (s.top()!=slow->val)                return false;            else {                slow = slow->next;                s.pop();            }        }        return true;    }};


0 0
原创粉丝点击