leetcode-2-两数相加

Posted nxzblogs

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode-2-两数相加相关的知识,希望对你有一定的参考价值。

 

问题:

技术图片

 

 

package com.nxz.blog.otherTest;

public class TestLeetCode 

    public static void main(String[] args) 

        TestLeetCode t = new TestLeetCode();
        ListNode l1 = new ListNode(2);
        ListNode l2 = new ListNode(4);
        ListNode l3 = new ListNode(3);
        ListNode r1 = new ListNode(5);
        ListNode r2 = new ListNode(6);
        ListNode r3 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        r1.next = r2;
        r2.next = r3;
        ListNode listNode = t.addTwoNumbers(l1, r1);

        do 
            System.out.println(listNode.val);
         while ((listNode = listNode.next )!= null);
    


    /**
     * 循环 l1 和 l2 两个listnode,当l1 或 l2 不为null的时候,将两个数相加,大于10时进行处理(保存进一位),否则视为0和另一个值相加
     *
     * @param l1
     * @param l2
     * @return
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) 
     //哑结点,最终返回的节点就是该节点的下一个节点 ListNode dummyHead
= new ListNode(-1); //暂存变量 ListNode p = l1, q = l2, curr = dummyHead; //大于10时的暂存值 int carry = 0; //当p或q不为null是,将值相加,此时需要处理,其中一个为null的情况 while (p != null || q != null) //只要p或q为nul时,将val值默认为0,和另一个值相加 int x = p != null ? p.val : 0; int y = q != null ? q.val : 0; int sum = carry + x + y; //重新设置进一变量 carry = sum / 10; curr.next = new ListNode(sum % 10); //重新设置当前节点,p,q节点,以便再一次循环 curr = curr.next; if (p != null) p = p.next; if (q != null) q = q.next; //最后处理最高为的carry的值 if (carry > 0) curr.next = new ListNode(carry); return dummyHead.next; public static class ListNode int val; ListNode next; ListNode(int x) val = x;

 

 

 进阶----->>>>

两数相加:节点为正序的情况

技术图片

package com.nxz.blog.otherTest;

import java.util.Stack;

public class TestLeetCode 

    public static void main(String[] args) 

        TestLeetCode t = new TestLeetCode();
        ListNode l1 = new ListNode(2);
        ListNode l2 = new ListNode(4);
        ListNode l3 = new ListNode(3);
        ListNode r1 = new ListNode(5);
        ListNode r2 = new ListNode(6);
        ListNode r3 = new ListNode(4);
        l1.next = l2;
        l2.next = l3;
        r1.next = r2;
        r2.next = r3;
        ListNode listNode = t.addTwoNumbers(l1, r1);

        do 
            System.out.println(listNode.val);
         while ((listNode = listNode.next) != null);
    


    /**
     * 利用栈这种数据结构(先进后出),这样就可以将正序的节点转换为倒叙的节点了,这样依次从两个栈中取出值(顺序就是个、十、百。。。)
     * 这样就转换为之前的那种方式了
     *
     * @param l1
     * @param l2
     * @return
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) 
        Stack<ListNode> stack1 = new Stack<>();
        Stack<ListNode> stack2 = new Stack<>();
        ListNode dummyHead = new ListNode(-1);
        ListNode p = l1, q = l2, head = dummyHead;
        while (p != null) 
            stack1.add(p);
            p = p.next;
        
        while (q != null) 
            stack2.add(q);
            q = q.next;
        

        p = stack1.pop();
        q = stack2.pop();


        int carry = 0;
        while (p != null || q != null) 
            int x = p != null ? p.val : 0;
            int y = q != null ? q.val : 0;
            int sum = carry + x + y;
            carry = sum / 10;
            ListNode last = head.next;
            head.next = new ListNode(sum % 10);
            head.next.next = last;

            p = stack1.isEmpty() ? null : stack1.pop();
            q = stack2.isEmpty() ? null : stack2.pop();

        

        if (carry > 0) 
            ListNode last = head.next;
            head.next = new ListNode(carry);
            head.next.next = last;
        

        return dummyHead.next;
    

    public static class ListNode 
        int val;
        ListNode next;

        ListNode(int x) 
            val = x;
        
    

 

以上是关于leetcode-2-两数相加的主要内容,如果未能解决你的问题,请参考以下文章

leetcode:2. 两数相加

[LeetCode] 2. 两数相加

leetcode 2. 两数相加

LeetCode 2.两数相加

python刷LeetCode:2.两数相加

LeetCode 2.两数相加