友情支持

如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜

支付宝

微信

有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。

wx jikerizhi

公众号的微信号是: jikerizhi因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。

2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
  /**
   * @author D瓜哥 · https://www.diguage.com
   * @since 2018-09-17 11:42
   */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//        ListNode h1 = l1;
//        ListNode h2 = l2;
//        ListNode mergeList = null;
//        int carryOver = 0;
//        while (Objects.nonNull(h1) && Objects.nonNull(h2)) {
//            int v1 = h1.val;
//            int v2 = h2.val;
//            int newVal = v1 + v2 + carryOver;
//            if (newVal >= 10) {
//                carryOver = 1;
//                newVal -= 10;
//            } else {
//                carryOver = 0;
//            }
//            ListNode listNode = new ListNode(newVal);
//            listNode.next = mergeList;
//            mergeList = listNode;
//            h1 = h1.next;
//            h2 = h2.next;
//        }
//
//        ListNode rest = null;
//        if (Objects.nonNull(h1)) {
//            rest = h1;
//        }
//        if (Objects.nonNull(h2)) {
//            rest = h2;
//        }
//
//        ListNode restHead = rest;
//        while (Objects.nonNull(rest) && carryOver > 0) {
//            int newVal = rest.val + carryOver;
//            if (newVal >= 10) {
//                carryOver = 1;
//                newVal -= 10;
//            } else {
//                carryOver = 0;
//            }
//            rest.val = newVal;
//            if (Objects.isNull(rest.next) && carryOver > 0) {
//                ListNode carryNode = new ListNode(carryOver);
//                rest.next = carryNode;
//                carryOver = 0;
//            }
//            rest = rest.next;
//        }
//        if (Objects.isNull(restHead) && carryOver > 0) {
//            restHead = new ListNode(carryOver);
//        }
//
//        while (Objects.nonNull(mergeList)) {
//            ListNode next = mergeList.next;
//            mergeList.next = restHead;
//            restHead = mergeList;
//            mergeList = next;
//        }
//
//        return restHead;
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
        ListNode result = l1;
        int digit = 0;
        while (Objects.nonNull(l1.next) && Objects.nonNull(l2.next)) {
            int sum = l1.val + l2.val + digit;
            l1.val = sum % 10;
            digit = sum / 10;
            l1 = l1.next;
            l2 = l2.next;
        }
        int sum = l1.val + l2.val + digit;
        l1.val = sum % 10;
        digit = sum / 10;
        if (Objects.isNull(l1.next) && Objects.nonNull(l2.next)) {
            l1.next = l2.next;
            l1 = l1.next;
        }
        ListNode tail = null;
        ;
        while (digit > 0 && Objects.nonNull(l1)) {
            tail = l1;
            sum = l1.val + digit;
            l1.val = sum % 10;
            digit = sum / 10;
            l1 = l1.next;
        }
        if (digit > 0) {
            tail.next = new ListNode(digit);
        }
        return result;
//        ListNode result = l1;
//        int digit = 0;
//        while (Objects.nonNull(l1.next) && Objects.nonNull(l2.next)) {
//            int sum = l1.val + l2.val + digit;
//            l1.val = sum % 10;
//            digit = sum / 10;
//            l1 = l1.next;
//            l2 = l2.next;
//        }
//        int sum = l1.val + l2.val + digit;
//        l1.val = sum % 10;
//        digit = sum / 10;
//        if (Objects.isNull(l1.next) && Objects.nonNull(l2.next)) {
//            l1.next = l2.next;
//            l1 = l1.next;
//        }
//        while (digit > 0 && Objects.nonNull(l1.next)) {
//            sum = l1.val + digit;
//            l1.val = sum % 10;
//            digit = sum / 10;
//            l1 = l1.next;
//        }
//        if (digit > 0) {
//            l1.next = new ListNode(digit);
//        }
//        return result;
    }