利口2
1、第一次见到hackerrank要自己写driver的,然后就真的自己写,出错了:
l1一支递归,到最后就没了。返回的l1就是个尾巴。所以要保持head的关系
用一个head,然后append才行
其实可以查
2、要额外加函数addToTheLast之类的:https://www.geeksforgeeks.org/sum-of-two-linked-lists/
https://java2blog.com/add-two-numbers-represented-by-linked-list-in-java/
3、输入输出的要求:https://support.hackerrank.com/hc/en-us/articles/1500009518662-Sample-Problem-Statement
自己写的辣鸡code:
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String args[]) throws Exception { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String numsString = br.readLine(); String numsString1 = numsString.split(",")[0]; String numsString2 = numsString.split(",")[1]; String[] splited1 = numsString1.split("\s+"); ListNode l1 = new ListNode(Integer.parseInt(splited1[0])); for (int i = splited1.length - 1; i >= 0; i--){ ListNode node = new ListNode(Integer.parseInt(splited1[i])); System.out.println("node = " + node.val); l1.next = node; l1 = l1.next; } System.out.println("l1.toString() = " + l1.toString()); String[] splited2 = numsString2.split("\s+"); ListNode l2 = new ListNode(Integer.parseInt(splited2[0])); for (int i = splited2.length - 1; i >= 0; i--){ ListNode node = new ListNode(Integer.parseInt(splited2[i])); System.out.println("node = " + node.val); l2.next = node; } System.out.println("l2.toString() = " + l2.toString()); ListNode c1 = l1; ListNode c2 = l2; ListNode sentinel = new ListNode(0); ListNode d = sentinel; int sum = 0; while (c1 != null || c2 != null) { sum /= 10; if (c1 != null) { sum += c1.val; c1 = c1.next; } if (c2 != null) { sum += c2.val; c2 = c2.next; } d.next = new ListNode(sum % 10); d = d.next; } if (sum / 10 == 1) d.next = new ListNode(1); System.out.println(sentinel.next.toString()); } } class ListNode { int val; ListNode next; public ListNode(int x){ val=x; } public String toString() { String result = val + " "; if (next != null) { result += next.toString(); } return result; } }
深井冰,明明是807,说是708。一个oa弄得这么吹毛求疵干什么。算了。
看了一眼系统设计: