Java数据结构与算法面试题-两数之和 作者:哇塞大嘴好帅

Java数据结构与算法面试题-两数之和 作者:哇塞大嘴好帅

作者:哇塞大嘴好帅

​ 哇塞大嘴好帥

1.题目 -该题目由LeetCode提供

ou are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

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

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2.完整代码

package com.dazuizui.两数相加;


/**
 * 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 contains a single digit. Add the two numbers and return the sum as a linked list.
 *
 * You may assume the two numbers do not contain any leading zero, except the number 0 itself.
 *
 * 来源:力扣(LeetCode)
 * 链接:https://leetcode-cn.com/problems/two-sum
 * 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
 * @author Yida·Yang
 * @Time   2020-11-25
 * @Text   链表管理
 */
public class Demo1 {
    public static void main(String[] args) {
        NodeAdmin nodeAdmin = new NodeAdmin();
        nodeAdmin.addNode(1);
        nodeAdmin.addNode(2);
        nodeAdmin.addNode(4);
        nodeAdmin.addNode(5);

        NodeAdmin nodeAdmin1 = new NodeAdmin();
        nodeAdmin1.addNode(1);
        nodeAdmin1.addNode(2);
        nodeAdmin1.addNode(4);
        nodeAdmin1.addNode(5);
        nodeAdmin1.selectLinkenList();

        Solution solution = new Solution();
        ListNode listNode = solution.addTwoNumbers(nodeAdmin.getHead(), nodeAdmin1.getHead());

    }
}

/**
 * 面试题 两数相加
 * @author Yida·Yang
 * @Time   2020-11-25
 * @Text   两数相加
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //创建头节点,尾巴节点
        ListNode head = null,foot = null;
        //进位
        int carry = 0;
        //和
        int sum = 0;

        //两数相加操作
        while(true){
            int num1 = l1 != null ? l1.val : 0;
            int num2 = l2 != null ? l2.val : 0;
            sum = num1 + num2 +carry;

            if(head == null){
                head = foot = new ListNode(sum % 10);
            }else {
                foot.next = new ListNode(sum % 10);
                foot = foot.next;
                System.out.println("sum:"+sum+" foot = "+foot.val);
            }

            carry = sum / 10;

            //指针下移动
            if (l1 != null){
                l1 = l1.next;
            }

            if (l2 != null){
                l2 = l2.next;
            }

            if(l2 == null && l1 == null){
                break;
            }

        }

        if (carry > 0){
            foot.next = new ListNode(carry) ;
            System.out.println(foot.next.val);
        }

        return head;
    }
}


/**
 * @author Yida·Yang
 * @Time   2020-11-25
 * @Text   链表管理
 */
class NodeAdmin{
    ListNode head = new ListNode();

    //获取当前节点
    public ListNode getHead(){
        return this.head;
    }

    //添加一个链表
    public void addNode(int val) {
        //创建临时遍历
        ListNode temp = head;

        //如果头节点为NULL
        while (true){
            if(temp.next == null){
                break;
            }

            temp = temp.next;
        }
        temp.next = new ListNode(val);
        System.out.println("添加temp:"+temp);
    }

    //浏览链表
    public void selectLinkenList(){
        //创建临时遍历
        ListNode temp = head.next;

        while (true){
            if (temp == null){
                break;
            }


            System.out.println(temp.val);
            temp = temp.next;
        }
    }
}


/**
 * @author Yida·Yang
 * @Time   2020-11-25
 * @Text   链表节点
 */
class ListNode {
    int val;
    ListNode next;
    ListNode() {}

    @Override
    public String toString() {
        return "ListNode{" +
                "val=" + val +
                ", next=" + next +
                '}';
    }

    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

3.代码解析

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 * @author  哇塞大嘴好帅
 * @blogurl www.dazuizui.com
 * @e-mail  y51288033@outlook.com
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //创建头节点,尾节点。
        ListNode head = null,foot = null;

        //进位
        int carry = 0;
        //和
        int sum   = 0;

        //两数相加操作
        while(true){
            int num1 = l1 != null ? l1.val : 0;
            int num2 = l2 != null ? l2.val : 0;
            sum = num1 + num2 + carry;

            //计算
            if(head == null){
                head = foot = new ListNode(sum % 10);
            }else{
                foot.next = new ListNode(sum % 10);
                foot = foot.next;
            }

            //计算进位数
            carry = sum / 10;

            //尾指针下移
            if(l1 != null){
                l1 = l1.next;
            }

            if(l2 != null){
                l2 = l2.next;
            }

            //如果其中有一者为NULL则跳出循环
            if(l1 == null && l2 == null){
                break;
            }
        }

        if(carry > 0){
            foot.next = new ListNode(carry);
        }

        return head;
    }
}

​ 首先定义头节点,还有临时变量(临时变量要永远指向最后一个节点),之后在定义两个遍历一个和一个进位,之后进入while循环 如果head == null 成立就代表是第一个节点,如果是第一个节点就把头和尾节点都赋值为sum % 10,如果不是第一个节点就临时变量的下一个节点 = new ListNode(sum % 10); 然后指针下移,之后就是指针下移判断,如果两者链表(l1 and l2)都为null 那么就代表两数相加操作已经做完了就跳出循环,跳出循环后判断是否有进位,如果有进位,就尾节点的下一个**= new ListNode(carry);** 之后返回头节点。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哇塞大嘴好帅(DaZuiZui)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值