2. Add Two Numbers

原题

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

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值