LeetCode-Microsoft-Add Two Numbers II

Posted IncredibleThings

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-Microsoft-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 do not contain any leading zero, except the number 0 itself.

Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.

Example:

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

可看成是Add Two NumbersReverse Linked List的综合. 先reverse在逐个add, 最后把结果reverse回来.

Time Complexity: O(n). reverse O(n), add O(n).

Space: O(1). reverse O(1), add O(1).

AC Java:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        if(l1 == null){
            return l2;
        }
        if(l2 == null){
            return l1;
        }
        l1 = reverseList(l1);
        l2 = reverseList(l2);
        
        ListNode cur = l1;
        int len1 = 0;
        while(cur != null){
            len1++;
            cur = cur.next;
        }
        
        cur = l2;
        int len2 = 0;
        while(cur != null){
            len2++;
            cur = cur.next;
        }
        
        ListNode dummy = new ListNode(0);
        if(len1 > len2){
            dummy.next = l1;
        }else{
            dummy.next = l2;
        }
        cur = dummy;
        int carry = 0;
        
        while(l1 != null || l2 != null){
            if(l1 != null){
                carry += l1.val;
                l1 = l1.next;
            }
            if(l2 != null){
                carry += l2.val;
                l2 = l2.next;
            }
            cur.next.val = carry%10;
            cur = cur.next;
            carry /= 10;
        }
        if(carry != 0){
            cur.next = new ListNode(1);
        }
        
        ListNode nxt = dummy.next;
        ListNode newHead = reverseList(nxt);
        nxt.next = null;
        return newHead;
    }
    private ListNode reverseList(ListNode head){
        if(head == null || head.next == null){
            return head;
        }
        ListNode tail = head;
        ListNode cur = head;
        ListNode pre;
        ListNode temp;
        while(tail.next != null){
            pre = cur;
            cur = tail.next;
            temp = cur.next;
            cur.next = pre;
            tail.next = temp;
        }
        return cur;
    }
}

 

以上是关于LeetCode-Microsoft-Add Two Numbers II的主要内容,如果未能解决你的问题,请参考以下文章

Twain

Linux下TIME_WAIT连接优化内核参数tcp_tw_reuse与tcp_tw_recycle区别与联系浅析

ASEMI代理TW6869-TA1-CRH瑞萨TW6869-TA1-CRH车规级

linux内核网络参数tcp_tw_recycle 和 tcp_tw_reuse 你搞清楚了吗?

tcp_tw_reuse 与 net.ipv4.tcp_tw_recycle

tw9912配置使用