算法编程算法数据结构与算法

LeetCode第二题Add Two Numbers

2016-03-12  本文已影响690人  Diffey

一、题目说明

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
查看原题;
查看我的Github项目地址;
题目:给定两个代表非负数的链表,链表中的节点分别代表个十百等位数,求这个两个数的和,结果也用链表表示。

二、解题

2.1 遍历两个链表

遍历两个链表,把各个位数相加,注意进位就可以了。
代码如下:

public static ListNode addTwoNumbersBySigLoop(ListNode l1, ListNode l2) {
    if (l1 == null) {
        return l2;
    } else if (l2 == null) {
        return l1;
    }
    ListNode list = null;
    ListNode next = null;
    int sum = 0;
    int b = 0;
    while (l1 != null || l2 != null) {
        if (l1 != null) {
            sum = l1.val;
            l1 = l1.next;
        }
        if (l2 != null) {
            sum += l2.val;
            l2 = l2.next;
        }
        sum += b;
        if (sum > 9) {
            sum -= 10;
            b = 1;
        } else {
            b = 0;
        }
        if (list == null) {
            list = new ListNode(sum);
            next = list;
        } else {
            next.next = new ListNode(sum);
            next = next.next;
        }
        sum = 0;
    }
    if (b == 1) {
        next.next = new ListNode(b);
    }
    return list;
}

2.2 遍历较短的链表

另一种方式只遍历较短的链表,剩下的较长链表可以直接添加。
代码如下:

if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        }
        ListNode list = null;
        ListNode next = null;
        ListNode cl1 = l1.next;
        ListNode cl2 = l2.next;
        while (true) {
            if (cl1 == null) {
                cl1 = l1;
                cl2 = l2;
                break;
            } else if (cl2 == null) {
                cl1 = l2;
                cl2 = l1;
                break;
            } else {
                cl1 = cl1.next;
                cl2 = cl2.next;
            }
        }

        int sum = 0;
        int b = 0;
        while (cl1 != null) {
            sum = cl1.val + cl2.val + b;
            cl1 = cl1.next;
            cl2 = cl2.next;

            if (sum > 9) {
                sum -= 10;
                b = 1;
            } else {
                b = 0;
            }

            if (list == null) {
                list = new ListNode(sum);
                next = list;
            } else {
                next.next = new ListNode(sum);
                next = next.next;
            }

            sum = 0;
        }

        next.next = cl2;
        while (b == 1) {
            if (cl2 == null) {
                next.next = new ListNode(b);
                break;
            }
            sum = cl2.val + b;
            if (sum > 9) {
                sum -= 10;
                b = 1;
            } else {
                b = 0;
            }
            cl2.val = sum;
            cl2 = cl2.next;
            next = next.next;
        }
        return list;

三、总结

主要考察对链表的操作,对链表这种数据结构的遍历、增、删等操作应该熟练。

上一篇 下一篇

猜你喜欢

热点阅读