You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8同
调整顺序即可
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { String s1 = toStr(l1); String s2 = toStr(l2); return toNode(addTowStr(s1, s2)); } private String toStr(ListNode node) { StringBuilder sr = new StringBuilder(); if (node == null) return sr.toString(); while (node != null) { sr.append(node.val); node = node.next; } return sr.toString(); } private ListNode toNode(String str) { if (str == null) return null; ListNode cur = new ListNode(str.charAt(0)-'0'); ListNode head = cur; for (int i=1; i0) ret.append(over); return ret.toString(); } }