add-two-numbers-ii

注意:有一种好的方法,是将链表倒转,然后依次相加。

但是,按照题目要求,用了不改变原链表的方法。

就是将两个链表增加到相同长度,然后递归相加,子函数返回后处理进位。

https://leetcode.com/problems/add-two-numbers-ii/

package com.company;

import java.util.*;

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}
class Solution {

    ListNode addTwo(ListNode l1, ListNode l2) {
        //System.out.printf("add two %d, %d \n", l1.val, l2.val);
        ListNode ret = new ListNode(l1.val + l2.val);
        if (l1.next != null && l2.next != null) {
            ret.next = addTwo(l1.next, l2.next);
            ret.val += ret.next.val / 10;
            ret.next.val = ret.next.val % 10;
        }
        else {
            ret.next = null;
        }
        return ret;
    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int l1len = 0;
        ListNode ln = l1;
        while (ln != null) {
            l1len++;
            ln = ln.next;
        }
        int l2len = 0;
        ln = l2;
        while (ln != null) {
            l2len++;
            ln = ln.next;
        }
        if (l1len < l2len) {
            ln = l1;
            l1 = l2;
            l2 = ln;
            l1len = l1len ^ l2len;
            l2len = l1len ^ l2len;
            l1len = l1len ^ l2len;
        }

        for (int i=0; i<l1len-l2len; i++) {
            ln = new ListNode(0);
            ln.next = l2;
            l2 = ln;
        }

        ln = addTwo(l1, l2);
        if (ln.val >= 10) {
            ListNode newHead = new ListNode(ln.val / 10);
            ln.val = ln.val % 10;
            newHead.next = ln;
            ln = newHead;
        }
        return ln;
    }
}

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello!");
        Solution solution = new Solution();

        ListNode l1 = new ListNode(7);
        ListNode l11 = new ListNode(2);
        ListNode l12 = new ListNode(4);
        ListNode l13 = new ListNode(3);
        l1.next = l11;
        l11.next = l12;
        l12.next = l13;
        ListNode l2 = new ListNode(5);
        ListNode l21 = new ListNode(6);
        ListNode l22 = new ListNode(4);
        l2.next = l21;
        l21.next = l22;
        ListNode ret = solution.addTwoNumbers(l1, l2);
        System.out.printf("Get ret: \n");
        while (ret != null) {
            System.out.printf("%d", ret.val);
            ret = ret.next;
        }
        System.out.println();

        /*Iterator<List<Integer>> iterator = ret.iterator();
        while (iterator.hasNext()) {
            Iterator iter = iterator.next().iterator();
            while (iter.hasNext()) {
                System.out.printf("%d,", iter.next());
            }
            System.out.println();
        }*/

        System.out.println();

    }
}
时间: 2024-12-18 01:18:42

add-two-numbers-ii的相关文章

LeetCode 445. 两数相加 II(Add Two Numbers II)

445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 进阶: 如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转. LeetCode445. Add Two Numbers II中等 示例: 输入: (7 -> 2 -> 4 -> 3) + (5 ->

445 Add Two Numbers II

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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 con

445. Add Two Numbers II ——while s1 or s2 or carry 题目再简单也要些测试用例

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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 con

[LeetCode] Add Two Numbers II 两个数字相加之二

You are given two linked lists representing two non-negative numbers. The most significant digit comes first 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 con

Leetcode刷题总结: 445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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

445. Add Two Numbers II 两个数字相加2

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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

【Leetcode】445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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

445. Add Two Numbers II - Medium

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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

LeetCode &quot;445. Add Two Numbers II&quot;

A natural stack based solution. Seriously, whey 'Medium'? /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* addTwoNumbers(Lis

[Lintcode] Add Two Numbers I &amp;&amp; II

Add Two Numbers You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and ret