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

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

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

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

package LeetCode;

import java.util.HashSet;
import java.util.Set;

/** * @author jinhuajian * @data 2018年12月19日---下午1:38:16 * @blog https://me.csdn.net/qq_37119939 */
class ListNode {
	int val;
	ListNode next;

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

public class Solution_02 {
	/** * 复杂度分析时间复杂度:O(max(m,n)),假设 m 和 n 分别表示 l1 和 l2 的长度, 上面的算法最多重复 max(m,n) * 次。空间复杂度:O(max(m,n)), 新列表的长度最多为 max(m,n)+1。 用例:1.[] [1,3,5] 2.[2,4,6],[8,2] */
	public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		if (l1 == null && l2 == null) {
			return null;
		}
		// 要考虑相加溢出的问题,必须要从低位相加,考虑两链表不等长,null设置为0
		ListNode head = new ListNode(0);
		ListNode h1 = l1, h2 = l2, cur = head;
		int carry = 0;
		while (h1 != null || h2 != null) {
			int x = (h1 == null) ? 0 : h1.val;
			int y = (h2 == null) ? 0 : h2.val;
			int sum = x + y + carry;
			carry = sum / 10;
			cur.next = new ListNode(sum % 10);
			cur = cur.next;
			if (h1 != null)
				h1 = h1.next;
			if (h2 != null)
				h2 = h2.next;
		}
		// 如果最高位要进一位
		if (carry > 0) {
			cur.next = new ListNode(carry);
		}
		return head.next;// 第一位是0,从第二位开始
	}

	public static void main(String[] args) {
		ListNode head = new ListNode(0), cur = head;
		int i = 1;
		while (i < 5) {
			ListNode next = new ListNode(i);
			cur.next = next;
			cur = cur.next;
			i++;
		}
		Solution_02 s2 = new Solution_02();
		ListNode result = s2.addTwoNumbers(head, head);
		while (result != null) {
			// System.out.println(result.val);
			result = result.next;
		}

		String str = "1334";
		char[] arr = str.toCharArray();
		Set<String> set = new HashSet<>();
		// set.add(str.charAt(1));
		System.out.print(str.charAt(1));

	}

}