LeetCode题库整理【Java】
2.两数相加
题目:给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例1:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
示例2:
输入:(9) + (1 -> 9-> 9-> 9-> 9-> 9 -> 9)
输出:1 -> 0 -> 0
原因:9 + 9999991 = 10000000
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head1 = l1;
ListNode head2 = l2;
ListNode head3 = new ListNode(0);
ListNode result =head3;
//进位标志
boolean flag = false;
while(head1!=null || head2!=null) {
//获取对应位置的值然后相加
int x = (head1!=null) ? head1.val : 0;
int y = (head2!=null) ? head2.val : 0;
int sum = flag ? x+y+1 : x+y;
//判断是否有新的进位
if( (sum/10) != 0) {
sum %= 10;
flag = true;
}else
flag = false;
//新增节点
result.next = new ListNode(sum % 10);
result = result.next;
if (head1 != null)
head1 = head1.next;
if (head2 != null)
head2 = head2.next;
}
//处理最后一位相加之后的进位
if(flag)
result.next=new ListNode(1);
return head3.next;
}
}
在自己的电脑软件上做测试时,可以将ListNode类写为内部类,在public ListNode addTwoNumbers(ListNode l1,ListNode l2) { }方法体中补全代码,在主函数main()入口中输入链表l1和l2的内容并生成链表,并将输出结果l3的内容打印出来。
完整的Java测试代码如下:
/**
* 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; }
* }
*/
package leet.code;
import java.util.Scanner;
public class AddTwoNumbers {
@SuppressWarnings("resource")
public static void main(String[] args) {
// TODO Auto-generated method stub
AddTwoNumbers add = new AddTwoNumbers();
AddTwoNumbers.ListNode head1 = add.new ListNode(0);
AddTwoNumbers.ListNode l1=head1;
AddTwoNumbers.ListNode head2 = add.new ListNode(0);
AddTwoNumbers.ListNode l2=head2;
AddTwoNumbers.ListNode head3 = add.new ListNode(0);
Scanner sc = new Scanner(System.in);
//在控制台输入链表l1各个节点的值,以逗号隔开
String str1 = sc.nextLine();
String[] num1 = str1.split(",");//用逗号隔开
int[] num1int = new int[num1.length];
for(int i=0;i<num1int.length;i++) {
num1int[i] = Integer.parseInt(num1[i]);
l1.next = add.new ListNode(num1int[i]);
l1 = l1.next;
}
head1 = head1.next;
//在控制台输入链表l2各个节点的值,以逗号隔开
String str2 = sc.nextLine();
String[] num2 = str2.split(",");//用逗号隔开
int[] num2int = new int[num2.length];
for(int i=0;i<num2int.length;i++) {
num2int[i] = Integer.parseInt(num2[i]);
l2.next = add.new ListNode(num2int[i]);
l2 = l2.next;
}
head2 = head2.next;
head3 = add.addTwoNumbers(head1, head2);
while(head3!=null) {
System.out.println(head3.val);
head3=head3.next;
}
}
public class ListNode { //建立内部类ListNode
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public ListNode addTwoNumbers(ListNode l1,ListNode l2) {
ListNode head1 = l1;
ListNode head2 = l2;
ListNode head3 = new ListNode(0);
ListNode result =head3;
//进位标志
boolean flag = false;
while(head1!=null || head2!=null) {
//获取对应位置的值然后相加
int x = (head1!=null) ? head1.val : 0;
int y = (head2!=null) ? head2.val : 0;
int sum = flag ? x+y+1 : x+y;
//判断是否有新的进位
if( (sum/10) != 0) {
sum %= 10;
flag = true;
}else
flag = false;
//新增节点
result.next = new ListNode(sum % 10);
result = result.next;
if (head1 != null)
head1 = head1.next;
if (head2 != null)
head2 = head2.next;
}
//处理最后一位相加之后的进位
if(flag)
result.next=new ListNode(1);
return head3.next;
}
}
参考网址:https://www.cnblogs.com/mfrank/p/10472639.html
该文章中还给出了很多人可能会踩坑的解题思路,并且给出了代码,也写得很有趣