2. Add Two Numbers

来源:互联网 发布:大闹天宫数据库密码 编辑:程序博客网 时间:2024/06/06 09:19

原题

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

代码实现

This is an in-place and time complexity O(n), space complexity O(1) algorithm

    // Definition for singly-linked list.    public class ListNode    {        public int val;        public ListNode next;        public ListNode(int x) { val = x; }    }    public class Solution    {        //This is in-place algorithm        private int carrybit;        public ListNode AddTwoNumbers(ListNode l1, ListNode l2)        {           //tmpl1: used to iterator; lastl1: points to lastnode of linkeslist             ListNode tmpl1 =l1, lastl1 = l1;             ListNode tmpl2 =l2, lastl2 = l2;            while (tmpl1 != null || tmpl2 != null || carrybit == 1)            {                if (tmpl1 == null) //if l1 first arrive end point                {                    int tmp = single(ref tmpl2,  ref lastl1, ref lastl2);                    if (tmp == -1) return l2;                    if (tmp == 1) continue;                }                if (tmpl2 == null) //symmetrical analysis                {                    int tmp = single(ref tmpl1, ref lastl2, ref lastl1);                    if (tmp == -1) return l1;                    if (tmp == 1) continue;                }                both(tmpl1, tmpl2);                lastl1 = tmpl1;                lastl2 = tmpl2;                tmpl1 = tmpl1.next;                tmpl2 = tmpl2.next;            }            return l2;         }        //assert tmpnode!=null        //-1: break calculation        //1: continue;        //0: go on        private int single(ref ListNode tmpnode, ref ListNode lastl1, ref ListNode lastl2)        {            if (tmpnode == null) //it indicates both linkedlist are null            {                if (carrybit == 1) //so only check whether carrybit is one                {                   //if happens, it would only happen once.                    var newnode = new ListNode(1); //this is the overflow bit                    lastl2.next = newnode; //let lastl2 points to this new node                    return -1;                }            }            else if (tmpnode != null) //tmp1==null && tmpl2!=null            {                int tmp = tmpnode.val + carrybit;                tmpnode.val = tmp;                if (tmp < 10) return -1; //all programe has ended.                tmpnode.val = tmp - 10; //if arriving here, it shows carrybit is one                //considering l1:[9,9]; l2:[9],                 //when iteratoring second bit, l2 is null and l1 is not null and its second bit is 9 which adds carrybit(1)                lastl1.next = tmpnode; //lastl1.next points to this bit                lastl1 = lastl1.next; //lastl1 iterator once                carrybit = 1;                lastl2 = tmpnode; //lastl2 saves current iteratoring node                tmpnode = tmpnode.next; //tmpnode contines to iterator                return 1;            }            return 0;        }        //assert tmpl1!=null && tmpl2!=null        private void both(ListNode tmpl1, ListNode tmpl2)        {            if (tmpl1.val + tmpl2.val + carrybit < 10) //no carrybit            {                int tmp = tmpl1.val;                tmpl1.val += tmpl2.val + carrybit;                tmpl2.val += tmp + carrybit;                carrybit = 0;            }            else //has carrybit            {                int tmp = tmpl1.val + tmpl2.val - 10 + carrybit;                tmpl1.val = tmp;                tmpl2.val = tmp;                carrybit = 1;            }        }    }

这里写图片描述

题库

Leetcode算法题目解决方案每天更新在github库中,欢迎感兴趣的朋友加入进来,也欢迎star,或pull request。https://github.com/jackzhenguo/leetcode-csharp

原创粉丝点击