[Lintcode] Add Two Numbers I && 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 returns the sum as a linked list.

Example

Given 7->1->6 + 5->9->2. That is, 617 + 295.

Return 2->1->9. That is 912.

Given 3->1->5 and 5->9->2, return 8->0->8.

SOLUTION:

很简单,就是对应加法,重新见一个linkedlist,然后还要考虑进位,要一个进位count,位数加法还要加上count。

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param l1: the first list
     * @param l2: the second list
     * @return: the sum list of l1 and l2
     */
    public ListNode addLists(ListNode l1, ListNode l2) {
        if (l1 == null){
            return l2;
        }
        if (l2 == null){
            return l1;
        }
        int count = 0;
        // int sum = 0;
        ListNode dummy = new ListNode(0);
        ListNode head = dummy;
        while (l1 != null && l2 != null){
            head.next = new ListNode((l1.val + l2.val + count) % 10);
            count =(l1.val + l2.val + count) / 10;
            l1 = l1.next;
            l2 = l2.next;
            head = head.next;
        }
        while (l1 != null){
            head.next = new ListNode((l1.val + count) % 10);
            count = (l1.val + count) / 10;
            // head = head.next;
            l1 = l1.next;
            head = head.next;
        }
        while (l2 != null){
            head.next = new ListNode((l2.val + count) % 10);
            count = (l2.val + count) / 10;
            // head = head.next;
            l2 = l2.next;
            head = head.next;
        }
        if (count != 0){
            head.next = new ListNode(count);
        }
        return dummy.next;
    }
}

FOLLOW UP:

Add Two Numbers II

You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in forward order, such that the 1‘s digit is at the head of the list. Write a function that adds the two numbers and returns the sum as a linked list.

Example

Given 6->1->7 + 2->9->5. That is, 617 + 295.

Return 9->1->2. That is, 912.

SOLUTION:

其实就是正向的操作,向正常的加法那样,不过我们既然会倒着的,正着肯定也会啊!我们就先reverse一下,把这些都倒过来,然后再用第一题方法加上,然后再把答案reverse回来,就OK啦!

代码:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param l1: the first list
     * @param l2: the second list
     * @return: the sum list of l1 and l2
     */
    public ListNode addLists2(ListNode l1, ListNode l2) {
        if (l1 == null){
            return l2;
        }
        if (l2 == null){
            return l1;
        }
        l1 = reverse(l1);
        l2 = reverse(l2);
        ListNode head = add(l1, l2);
        return reverse(head);
    }
    private ListNode reverse(ListNode head){
        if (head == null){
            return null;
        }
        ListNode pre = null;
        while (head != null){
            ListNode temp = head.next;
            head.next = pre;
            pre = head;
            head = temp;
        }
        return pre;
    }
    private ListNode add(ListNode l1, ListNode l2){
        if (l1 == null){
            return l2;
        }
        if (l2 == null){
            return l1;
        }
        int sum = 0;
        int count = 0;
        ListNode dummy = new ListNode(0);
        ListNode curr = dummy;
        while (l1 != null && l2 != null){
            sum = l1.val + l2.val + count;
            curr.next = new ListNode(sum % 10);
            count = sum / 10;
            curr = curr.next;
            l1 = l1.next;
            l2 = l2.next;
        }
        while (l1 != null){
            sum = l1.val + count;
            curr.next = new ListNode(sum % 10);
            count = sum / 10;
            curr = curr.next;
            l1 = l1.next;
        }
        while (l2 != null){
            sum = l2.val + count;
            curr.next = new ListNode(sum % 10);
            count = sum / 10;
            curr = curr.next;
            l2 = l2.next;
        }
        if (count != 0){
            curr.next = new ListNode(count);
        }
        return dummy.next;
    }
}

时间: 2024-11-05 22:31:50

[Lintcode] Add Two Numbers I && II的相关文章

Lintcode: Add Two Numbers

C++ 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 /** 12 * @param l1: the first list 13 * @param l2: the se

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 ->

2、Add Two Numbers

1.Add Two Numbers--这是leedcode的第二题: 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:

LeetCode --- 2. Add Two Numbers

题目链接: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

【LeetCode】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 ->

Add Two Numbers(Linked List)

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) + (

Add Two Numbers

题目: 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. 程序设计 链表节点定义 Def

Leetcode 02 Add Two Numbers

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) + (

【Leet Code】Add Two Numbers

Add Two Numbers Total Accepted: 20255 Total Submissions: 88115My Submissions 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 numb