2. Add Two Numbers(链表尾插法)
发布日期:2021-06-30 19:56:01
浏览次数:2
分类:技术文章
本文共 4485 字,大约阅读时间需要 14 分钟。
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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)Output: 7 -> 0 -> 8Explanation: 342 + 465 = 807.
/** * 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) { ListNode list = null, s = null; int sum = 0; while (l1 != null || l2 != null) { sum /= 10; if (l1 != null) { sum += l1.val; l1 = l1.next; } if (l2 != null) { sum += l2.val; l2 = l2.next; } if (list == null) { list = new ListNode(sum % 10); s = list; } else { ListNode node = new ListNode(sum % 10); s.next = node; s = s.next; } } if (sum / 10 == 1) { s.next = new ListNode(1); } return list; }}
Debug code in playground:
/* ----------------------------------- * WARNING: * ----------------------------------- * Your code may fail to compile * because it contains public class * declarations. * To fix this, please remove the * "public" keyword from your class * declarations. *//** * 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) { ListNode list = null, s = null; int sum = 0; while (l1 != null || l2 != null) { sum /= 10; if (l1 != null) { sum += l1.val; l1 = l1.next; } if (l2 != null) { sum += l2.val; l2 = l2.next; } if (list == null) { list = new ListNode(sum % 10); s = list; } else { ListNode node = new ListNode(sum % 10); s.next = node; s = s.next; } } if (sum / 10 == 1) { s.next = new ListNode(1); } return list; }}public class MainClass { public static int[] stringToIntegerArray(String input) { input = input.trim(); input = input.substring(1, input.length() - 1); if (input.length() == 0) { return new int[0]; } String[] parts = input.split(","); int[] output = new int[parts.length]; for(int index = 0; index < parts.length; index++) { String part = parts[index].trim(); output[index] = Integer.parseInt(part); } return output; } public static ListNode stringToListNode(String input) { // Generate array from the input int[] nodeValues = stringToIntegerArray(input); // Now convert that list into linked list ListNode dummyRoot = new ListNode(0); ListNode ptr = dummyRoot; for(int item : nodeValues) { ptr.next = new ListNode(item); ptr = ptr.next; } return dummyRoot.next; } public static String listNodeToString(ListNode node) { if (node == null) { return "[]"; } String result = ""; while (node != null) { result += Integer.toString(node.val) + ", "; node = node.next; } return "[" + result.substring(0, result.length() - 2) + "]"; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line = in.readLine()) != null) { ListNode l1 = stringToListNode(line); line = in.readLine(); ListNode l2 = stringToListNode(line); ListNode ret = new Solution().addTwoNumbers(l1, l2); String out = listNodeToString(ret); System.out.print(out); } }}
这个不要理所当然想成了头插法,看到测试代码才知道是尾插法,返回的ListNode也是需要尾插法的。
========================Talk is cheap, show me the code=========================
转载地址:https://liuchenyang0515.blog.csdn.net/article/details/82817708 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月07日 20时06分00秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
图像处理学习笔记
2021-07-03
Machine Learning Onramp on MATLAB 学习笔记
2021-07-03
Machine Learning with MATLAB 1.1 to 2.2
2021-07-03
Learning DSP with MATLAB
2021-07-03
用MATLAB实现m序列的生成(MATLAB 2021a适用)
2021-07-03
MATLAB函数备忘(定期更新)
2021-07-03
13行MATLAB代码实现网络爬虫 爬取NASA画廊星图
2021-07-03
MATLAB指定路径保存图片方法
2021-07-03
Python一键获取微信推送封面图
2021-07-03
油猴脚本:微信推送浏览功能拓展
2021-07-03
JavaScript DOM对象操作详解
2021-07-03
JavaScript 表单操作与MD5加密
2019-04-30
JAVA学习笔记4 - 循环与分支结构
2019-04-30
JAVA学习笔记6 - 数组
2019-04-30
JAVA学习笔记8 - Stream 和 File I/O
2019-04-30
JAVA学习笔记9 - 异常
2019-04-30
JAVA学习笔记10 - 继承
2019-04-30
JAVA学习笔记11 - 接口interface
2019-04-30
JAVA学习笔记12 - 包package
2019-04-30