力扣题目---两数之和C#版本

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
        
        ListNode nextListNde = new ListNode(0,null);
        ListNode newListNde = new ListNode(0,null) ;
        while(l1 != null || l2 != null )
        {
            
            int num1 = l1 != null ? l1.val:0;
            int num2 = l2 != null? l2.val:0;
            int allNum = num1 + num2;
            if(newListNde.next == null)
            {
                newListNde.val = allNum;
                newListNde.next = nextListNde;
                l1 = l1 != null ? l1.next : null;
                l2 = l2 != null? l2.next : null;
                continue;
            }
            ListNode tempNode = new ListNode(0,null);
            nextListNde.val = allNum;
            Console.WriteLine(allNum);
            l1 = l1 != null ? l1.next : null;
            l2 = l2 != null? l2.next : null;
            if(l1 != null || l2 != null )
            {
                nextListNde.next = tempNode;
                nextListNde = tempNode;
            }
        }
        ListNode tempListNde = newListNde.next ;
        while (tempListNde !=null)
        {
            
            //Console.WriteLine(tempListNde.val);
            if(tempListNde.val >= 10)
            {
                tempListNde.val = tempListNde.val % 10;  
                if (tempListNde.next == null)
                {
                    ListNode tempNode = new ListNode(0,null);
                    tempListNde.next = tempNode;
                }
                else
                {
                    tempListNde.next.val += 1;
                }
            }
            tempListNde = tempListNde.next;
        }
        return newListNde;
    }
}

第二种写法

public class Solution {
    public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
            if(l1 == null) return l2;
            if(l2 == null) return l1;
            int num = l1.val + l2.val;
            ListNode newListNode = new ListNode(num,null);
            //这里不用判断两个列表的下一个是否都为空,因为上面的和以及下面的判断都完美的规避了这个问题
            //if(l1.next == null && l2.next == null)
              //return new ListNode(num,null);
            //ListNode newListNode = new ListNode(num,null);
            ListNode nextlistNode  = newListNode;
            while(l1.next != null && l2.next != null)
            {
                //int allNum = l1.next.val + l2.next.val;
                ListNode temp = new ListNode(l1.next.val + l2.next.val,null);
                nextlistNode.next = temp;
                nextlistNode = temp;
                l1 = l1.next;
                l2 = l2.next; 
            }
                      if(l1.next == null)
            {
                if(l2.next != null)
                {
                    nextlistNode.next = l2.next;
                }
            }
            else if(l2.next == null)
            {
                if(l1.next != null)
                {
                   nextlistNode.next = l1.next;
                }
            }
            ListNode tempList = newListNode;
            while(tempList != null)
            {
                Console.WriteLine(tempList.val);
                if(tempList.val >= 10)
                {
                   
                    if(tempList.next == null)
                    {
                        ListNode tempNodeList = new ListNode(1,null);
                        tempList.val = tempList.val % 10;
                        tempList.next = tempNodeList;
                        tempList = tempNodeList;
                    }
                    else
                    {
                        tempList.val = tempList.val % 10;
                        tempList.next.val += 1;
                        
                    }
                }
                tempList = tempList.next;
            }
            return newListNode;
    }
}



**力扣大佬又短又细的解题**
public class Solution {
    public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
        if (l1 == null && l2 == null) return null; // 两个都为null 退出递归
        int val = (l1?.val ?? 0) + (l2?.val ?? 0); // 两数相加
        int carry = val / 10; // 进位数
        if (carry > 0) {
            val = val % 10; // 需要进位的情况 除以10的余数作为当前值
            // 进位数给l1或者l2都行 这里给l1
            if (l1.next == null) {
                l1.next = new ListNode(carry); // next为空的情况 实例化一个ListNode 值为进位数
            } else {
                l1.next.val += carry; // next不为空 值为原值与进位数相加之和
            }
        }
        return new ListNode(val, AddTwoNumbers(l1?.next, l2?.next)); // 递归入口为构造的next参数
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值