LeedCode刷题系列0002——两数相加

问题:

给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。

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


 

问题示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807

解决方案:

这个问题对于java而言有些尴尬,因为java现在没有ListNode这个类...

所以我们需要自己创建这个简单的类,这是一个链表节点的类。即包含值和一个next(相当于在C++中的next指针)。

ListNode类如下(注意此类应该和解决方案放在一个文件夹中,而不是一个文件中)

package com.lmm.LeedCode;

/**
 * @author lmm E-mail:violet_mmhh@163.com
 * @version 时间:2018年9月26日
 * @function 创建ListNode类
 */
public class ListNode {
	int val;
	ListNode next;

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

}

在解决方法中,主要就是根据我们的加减运算来的。

package com.lmm.LeedCode;

import java.util.Scanner;

/**
 * @author lmm E-mail:violet_mmhh@163.com
 * @version 时间:2018年9月26日
 * @question 给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。
 * @question 你可以假设除了数字 0 之外,这两个数字都不会以零开头。
 */
public class Solution0002 {
	public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		ListNode dummyHead = new ListNode(0);
		ListNode p = l1, q = l2, curr = dummyHead;
		int carry = 0;// 进位
		while (p != null || q != null) {
			int x = (p != null) ? p.val : 0;
			int y = (q != null) ? q.val : 0;
			int sum = x + y + carry;
			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 > 0) {
			curr.next = new ListNode(carry);
		}
		return dummyHead.next;
	}

	public static void main(String[] args) {
		ListNode l1 = null;
		ListNode newNode = null;
		ListNode last = null;
		Scanner in = new Scanner(System.in);
		int val = in.nextInt();
		String s1 = Integer.toString(val);
		char[] ch = s1.toCharArray();
		for (int i = ch.length - 1; i >= 0; i--) {
			int num = ch[i] - '0';
			newNode = new ListNode(num);
			if (l1 == null) {
				l1 = newNode;
				last = newNode;
			} else {
				last.next = newNode;
				last = newNode;
			}
		}
		ListNode l2 = null;
		val = in.nextInt();
		String s2 = Integer.toString(val);
		char[] ch2 = s2.toCharArray();
		for (int i = ch2.length - 1; i >= 0; i--) {
			int num = ch2[i] - '0';
			newNode = new ListNode(num);
			if (l2 == null) {
				l2 = newNode;
				last = newNode;
			} else {
				last.next = newNode;
				last = newNode;
			}
		}
		ListNode l3 = addTwoNumbers(l1, l2);
		ListNode head = l1;
		System.out.print("你输入的第1个数:");
		while (head != null) {
			System.out.print(head.val);
			head = head.next;
			if (head != null) {
				System.out.print("-->");
			}
		}
		System.out.println();
		System.out.print("你输入的第2个数:");
		ListNode head2 = l2;
		while (head2 != null) {
			System.out.print(head2.val);
			head2 = head2.next;
			if (head2 != null) {
				System.out.print("-->");
			}
		}
		System.out.println();
		System.out.print("结果:");
		ListNode head3 = l3;
		while (head3 != null) {
			System.out.print(head3.val);
			head3 = head3.next;
			if (head3 != null) {
				System.out.print("-->");
			}
		}
	}
}

运行结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值