免费空间网站php,搜索框html代码,温州论坛吧,二手交易网站建设目标论代码能力#xff1a;
给出两个 非空 的链表用来表示两个非负的整数。其中#xff0c;它们各自的位数是按照 逆序 的方式存储的#xff0c;并且它们的每个节点只能存储 一位 数字。
如果#xff0c;我们将这两个数相加起来#xff0c;则会返回一个新的链表来表示它们的…论代码能力
给出两个 非空 的链表用来表示两个非负的整数。其中它们各自的位数是按照 逆序 的方式存储的并且它们的每个节点只能存储 一位 数字。
如果我们将这两个数相加起来则会返回一个新的链表来表示它们的和。
您可以假设除了数字 0 之外这两个数都不会以 0 开头。
示例
输入(2 - 4 - 3) (5 - 6 - 4)
输出7 - 0 - 8
原因342 465 807主方法为addTwoNumbers
自己最初的解题代码 public class Test {public static void main(String[] args) throws InterruptedException {
// 输入(2 - 4 - 3) (5 - 6 - 4)
// 输出7 - 0 - 8
// 原因342 465 807
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
// [5,6,4]ListNode aNode initNode(new int[]{5});ListNode bNode initNode(new int[]{5});ListNode cNode addTwoNumbers(aNode, bNode);System.out.println(JSONObject.toJSONString(cNode));}public static ListNode initNode(int [] arr) {ListNode head new ListNode(arr[0]);ListNode node head;for(int i1;iarr.length;i) {ListNode tNode new ListNode(arr[i]);node.next tNode;node node.next;}return head;}public static class ListNode {int val;ListNode next;public ListNode(int val) {this.val val;}}public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {ListInteger arr1 new ArrayListInteger();ListInteger arr2 new ArrayListInteger();ListNode t1 l1;ListNode t2 l2;while (t1!null) {arr1.add(t1.val);t1 t1.next;}while (t2!null) {arr2.add(t2.val);t2 t2.next;}ListInteger arr3 null;arr3 getResultList(arr1.size() arr2.size()?arr1:arr2, arr1.size() arr2.size()?arr2:arr1);ListNode head new ListNode(arr3.get(0));ListNode node head;for(int i1;iarr3.size();i) {ListNode tNode new ListNode(arr3.get(i));node.next tNode;node node.next;}return head;}public static ListInteger getResultList(ListInteger longList, ListInteger shortList) {ListInteger arr3 new ArrayListInteger();int jinJi 0;for(int i0;ilongList.size();i) {int a longList.get(i);int b0;if(ishortList.size()) {bshortList.get(i);}int count ab jinJi;if(count9) {jinJi 1;}else {jinJi 0;}arr3.add(count % 10);}if(jinJi ! 0) {arr3.add(jinJi);}return arr3;}}参照标准答案重写的代码
public class Test {public static void main(String[] args) throws InterruptedException {
// 输入(2 - 4 - 3) (5 - 6 - 4)
// 输出7 - 0 - 8
// 原因342 465 807
// [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
// [5,6,4]ListNode aNode initNode(new int[]{5});ListNode bNode initNode(new int[]{5});ListNode cNode addTwoNumbers(aNode, bNode);System.out.println(JSONObject.toJSONString(cNode));}public static ListNode initNode(int [] arr) {ListNode head new ListNode(arr[0]);ListNode node head;for(int i1;iarr.length;i) {ListNode tNode new ListNode(arr[i]);node.next tNode;node node.next;}return head;}public static class ListNode {int val;ListNode next;public ListNode(int val) {this.val val;}}public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {//哑节点ListNode yaNode new ListNode(0);ListNode head yaNode;int jinJi 0;int benJi 0;while (l1!null || l2!null) {int temp (l1 null?0:l1.val) (l2null?0:l2.val) jinJi;jinJi temp9?1:0;benJi temp % 10;ListNode node new ListNode(benJi);head.next node;head head.next;if(l1 ! null) {l1 l1.next;}if(l2 ! null) {l2 l2.next;}}if(jinJi ! 0) {ListNode node new ListNode(jinJi);head.next node;}return yaNode.next;}
部分思路按照人类解题加法超过9进级的思路是类似的但是局部进行了优化不用借助ArrayList直接使用链表遍历即可 减少了内存占用并且代码更加的简洁易懂不易出错。继续努力吧