leetcode(2) Add Two Numbers

来源:互联网 发布:发改委大数据专项2016 编辑:程序博客网 时间:2024/06/13 16:42

题目链接:https://leetcode.com/problems/add-two-numbers/

You are given two linked lists representing two non-negative numbers. 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

Subscribe to see which companies asked this question

带进位的列表相加,2+5=7,4+6=10,进位1,第二个数为0,3+4+进位1第三个数为8.

这道题比较简单,对每个表的同位进行相加然后求/,放到新列表中即可

java代码:

package leetcode;public class AddTwoNumbers {// https://leetcode.com/problems/add-two-numbers/// Definition for singly-linked list.public static class ListNode {int val;ListNode next;ListNode(int x) {val = x;}}public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {int carry = 0;ListNode newHead = new ListNode(0);ListNode p1 = l1, p2 = l2, p3 = newHead;while (p1 != null || p2 != null) {if (p1 != null) {carry += p1.val;p1 = p1.next;}if (p2 != null) {carry += p2.val;p2 = p2.next;}p3.next = new ListNode(carry % 10);p3 = p3.next;carry /= 10;}if (carry == 1)p3.next = new ListNode(1);return newHead.next;}public static void main(String agrs[]) {ListNode l1 = new ListNode(0), l2 = new ListNode(0);l1.val = 2;l1.next = new ListNode(4);l1.next.next = new ListNode(3);l2.val = 5;l2.next = new ListNode(6);l2.next.next = new ListNode(4);ListNode result = addTwoNumbers(l1, l2);while (result.next != null) {System.out.println(result.val);result = result.next;}System.out.println(result.val + "");}}

0 0
原创粉丝点击