Add Two Numbers

原题 https://leetcode.com/problems/add-two-numbers/description/

题意: 给定两个链表代表两个非负整数,链表的每个节点表示整数的一位,存储的顺序逆序(如:百位->十位->个位),求两个整数之和用同样的链表表示。

约定:(1)整数第一位不会包含数字0,除非该整数为0

例子:

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


标签: 整数、numbers、链表、百位、十位、面试
猜你感兴趣的圈子:
LeetCode交流圈
  • 刀神T
    2017-08-08 23:48:11 1楼#1层
    public class Solution {
    
        static class ListNode {
            int val;
    
            ListNode next;
    
            ListNode(int x) {
                val = x;
            }
        }
    
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    
            ListNode cur1 = l1;
            ListNode cur2 = l2;
    
            int sum = cur1.val + cur2.val;
            int hasCarry = 0;
            if (sum >= 10) {
                hasCarry = 1;
                sum -= 10;
            }
    
            ListNode result = new ListNode(sum);
    
            cur1 = cur1.next;
            cur2 = cur2.next;
    
            ListNode cur = result;
    
            while (cur1 != null && cur2 != null) {
                sum = cur1.val + cur2.val + hasCarry;
                if (sum >= 10) {
                    hasCarry = 1;
                    sum -= 10;
                } else {
                    hasCarry = 0;
                }
    
                ListNode tmp = new ListNode(sum);
                cur.next = tmp;
                cur = tmp;
    
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
    
            while (cur1 != null) {
                sum = cur1.val + hasCarry;
                if (sum >= 10) {
                    hasCarry = 1;
                    sum -= 10;
                } else {
                    hasCarry = 0;
                }
    
                ListNode tmp = new ListNode(sum);
                cur.next = tmp;
                cur = tmp;
                cur1 = cur1.next;
            }
    
            while (cur2 != null) {
                sum = cur2.val + hasCarry;
                if (sum >= 10) {
                    hasCarry = 1;
                    sum -= 10;
                } else {
                    hasCarry = 0;
                }
    
                ListNode tmp = new ListNode(sum);
                cur.next = tmp;
                cur = tmp;
                cur2 = cur2.next;
            }
    
            if (hasCarry > 0) {
                ListNode tmp = new ListNode(hasCarry);
                cur.next = tmp;
            }
    
            return result;
    
        }
    
        public static void main(String[] args) {
            //        Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
            //        Output: 7 -> 0 -> 8
            ListNode l1 = new ListNode(2);
            ListNode tmp = new ListNode(4);
            l1.next = tmp;
            tmp = new ListNode(3);
            l1.next.next = tmp;
    
            ListNode l2 = new ListNode(5);
            tmp = new ListNode(6);
            l2.next = tmp;
            tmp = new ListNode(4);
            l2.next.next = tmp;
    
            ListNode result = new Solution().addTwoNumbers(l1, l2);
            while (result != null) {
                System.out.print(result.val + " ");
                if (result != null)
                    result = result.next;
            }
    
        }
    }
  • 回复
隐藏