leetcode刷题记录-List之两数求和以及变种问题

343 阅读3分钟

两数求和-题目描述

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。

如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。

您可以假设除了数字 0 之外,这两个数都不会以 0 开头。

题目链接

解题思路

  • 先设置一个返回链表的头结点result,然后设置一个可以移动的结点curr也指向result,将进位位carry初始化为0
  • 设置p、q分别初始化为链表l1和l2的头部
  • 循环遍历l1和l2,直到到达各自的尾端
    • 将x设置为结点p的值,如果p已经到达l1的末尾,则将其设置为0
    • 将y设置为结点q的值,如果q已经到达l2的末尾,则将其设置为0
    • 设置sum=x+y+carry
    • 创建出一个数值为sum%10的新结点,将其设置为当前结点的下一个结点,并将当前结点移动到下一个结点(尾插法),同时p和q都移动到下一个结点
    • 更新carry的值为sum/10
  • 检查carry的值是否是1(即最高位是否产生了进位,如果是的话,则向curr链表追加一个为1的新结点)
  • 返回result结点的下一个结点-求和链表的头结点
package LT;/*
 * Created by lizeyu on 2020/9/12 12:20
 */

public class AddTwoNumber {
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result = new ListNode(0);
        ListNode p = l1, q = l2, curr = result;
        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;
            curr.next = new ListNode(sum % 10);
            curr = curr.next;
            if (p != null) {
                p = p.next;
            }
            if (q != null) {
                q = q.next;
            }

        }
        if (carry == 1) {
            curr.next = new ListNode(1);
        }
        return result.next;
    }
}

两数求和Ⅱ-题目描述

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

题目链接

解题思路

  • 此题利用栈-栈存结点值,利用栈先进后出和后进先出的特性,将两数从低位开始相加
  • 分别遍历两个链表,将其值从高位到低位压入到两个不同的栈p和q中
  • carry记录进位位的值。head记录结果链表的头部
  • 分别遍历两个记录结点值的栈,直到到达栈底
    • 将x设置为栈p弹出栈顶的值,如果p已经到达栈底,则将其设置为0
    • 将y设置为栈q弹出栈顶的值,如果q已经到达栈底,则将其设置为0
    • 置sum=x+y+carry
    • 创建出一个数值为sum%10的新结点,将其下一个结点设置头结点head,再将head移动到当前结点(头插法)
    • 更新carry的值为sum/10
  • 检查carry的值是否是1(即最高位是否产生了进位,如果是的话,则向结果链表头插追加一个为1的新结点)
  • 返回head结点(结果链表的头结点)
package LT;/*
 * Created by lizeyu on 2020/9/12 12:21
 */

import java.util.Stack;

public class AddTwoNumber2 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        Stack<Integer> p = new Stack<Integer>();
        Stack<Integer> q = new Stack<Integer>();
        while (l1 != null) {
            p.push(l1.val);
            l1 = l1.next;

        }
        while (l2 != null) {
            q.push(l2.val);
            l2 = l2.next;

        }
        int carry = 0;
        ListNode head = null;
        while (!p.isEmpty() || !q.isEmpty()) {
            int x = p.isEmpty() ? 0 : p.pop();
            int y = q.isEmpty() ? 0 : q.pop();
            int sum = carry + x + y;
            ListNode temp = new ListNode(sum % 10);
            temp.next = head;
            head = temp;
            carry = sum / 10;
        }
        if (carry == 1) {
            ListNode temp = new ListNode(1);
            temp.next = head;
            head = temp;
        }
        return head;
    }
}