两个链表生成相加链表

98 阅读2分钟

445. 两数相加 II

Difficulty: 中等

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

进阶:

如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。

示例:

输入:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 8 -> 0 -> 7

Solution

Language: ****

/**
 * 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) {
          //937 + 63 = 1000
        //从后往前加
        if(l1 == null) return l2;
        if(l2 == null) return l1;
        ListNode current = l1;
        int sum1 = 0;
        while(current != null)
        {
            sum1 = sum1 * 10 + current.val;
            current = current.next;
        }
        current = l2;
        int sum2 = 0;
        while(current != null)
        {
            sum2 = sum2 * 10 + current.val;
            current = current.next;
        }
        int sum = sum1 + sum2;
        
        List<Integer> list = new ArrayList();
        while(sum != 0)
        {
            int tmp = sum % 10 ;
            list.add(tmp);
            sum = sum / 10;
            
        }
        //然后构造一颗新链表
        ListNode dummy = new ListNode(0);
        if(list.size() == 0)
        {
            return dummy;
        }
        
        ListNode first = dummy;
        for(int i = list.size() - 1; i >= 0; i--)
        {
            first.next = new ListNode(list.get(i));
            first = first.next;
        }
        first.next = null;
        return dummy.next;
    }
}
  • 这种方式是去把两个链表的值换成int,然后相加,

输入: [3,9,9,9,9,9,9,9,9,9] [7] 输出: [-2,-9,-4,-9,-6,-7,-2,-9,0] 预期: [4,0,0,0,0,0,0,0,0,6]

  • 这种都是会越界的; 所以这种方式是不可取的;

借助ArrayList或者Stack,我们可以实现链表的倒序相加


/**
 * 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) {
          //937 + 63 = 1000
        //从后往前加
        if(l1 == null && l2 == null) return null;
        List<Integer> list1 = new ArrayList();
        ListNode current = l1;
        while(current != null)
        {
            list1.add(current.val);
            current = current.next;
        }
        current = l2;
        List<Integer> list2 = new ArrayList();
        while(current != null)
        {
            list2.add(current.val);
            current = current.next;
        }
        int i = list1.size() - 1;
        int j = list2.size() - 1;
        int carry = 0;
      
        ListNode head = null;

        while(i >= 0 || j >= 0 || carry !=0)
        {
            int s1 = i < 0 ? 0 : list1.get(i);
            int s2 = j < 0 ? 0 : list2.get(j);
            int sum = s1 + s2 + carry;
            ListNode node = new ListNode(sum % 10);
            node.next = head;
            head = node;
            carry = sum / 10;
            i--;
            j--;
        }
        return head;
    }
}