Adding two numbers (two)

We are given two non-empty list is used to represent two non-negative integer. Where their respective bits are stored in reverse order of the way, and they each node can store only one digit.

If we add up these two numbers, it will return a new list and to represent them.

You may assume that in addition to 0, these two numbers will not begin with 0.

Example:

Input :( 2 -> 4 -> 3 ) + ( . 5 -> . 6 -> 4 )
Output: . 7 -> 0 -> 8 
Cause: 342 + 465 = 807

A Solution

Ideas:

  • Tags: list
  • The two lists are the same as the length of the traversal, if a shorter list at a leading zero, such as 987 + 23 = 987 + 023 = 1010
  • At the same time every computing need to consider the issue on a carry, but also need to update the current value after the end of the carry-bit computing
  • If the two lists all of the traversal is complete, the carry value is 1, the forefront in the new list to add the node 1
  • Tip: For list problem, return the results for the first node, generally need to initialize a predetermined pointer pre, this pointer is a node pointing to the real header node head using pre pointer object list initialization when no available node values, and the list construction process need to move the pointer, and thus will lead to the loss of the head pointer, can not return results

Below us one illustration above process:

(1) pre initialize pointer, and so cur point to the head node also points head, carry and carry initialized to 0

(2) Using the above ideas l1.value + l2.value + carry, 10 and take the remainder, rounding

(3) using the 7 + 3 = 10, carry = 1, sum = 0, and translate node l1, l2, cur

(4) After l1.value + l2.value + carry = 11, carry = sum / 10 = 1, bit value 1 into the residual values ​​of sum% 10 = 1

(5) After the calculation, we begin to translate l1, l2, cur

(6) After l1.value + l2.value + carry = 11, carry = sum / 10 = 1, bit value 1 into the residual values ​​of sum% 10 = 0 (the actual list is stored)

Translation l1, l2, cur after (7) is calculated, not the number of bits of 0s

(8) the end of the linked list traversal, the carry is 1, the node is added, and the head node is removed

(9) after removal of the head node 0, i.e. the result is 0101, 1010 is reversed to meet the requirements.

The entire code below:

public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
 }

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode(0);
        ListNode cur = pre;
        int carry = 0;
        while(l1 != null || l2 != null) {
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            int sum = x + y + carry;
            
            carry = sum / 10;
            sum = sum % 10;
            cur.next = new ListNode(sum);

            cur = cur.next;
            if(l1 != null)
                l1 = l1.next;
            if(l2 != null)
                l2 = l2.next;
        }
        if(carry == 1) {
            cur.next = new ListNode(carry);
        }
        return pre.next;
    }
}

 

解法二

抛弃解法一的思想:

  • 新建节点的class,包括该节点的值以及指向下一个节点的指针
  • 新建链表result,用于输出结果
  • 此不需要进位值标志carry,而是如果total > 10,让l1.value加1(或者l2.value加1),然后循环遍历即可

首先我们新建节点的class,如下:

public class ListNode {
    public var val: Int
    public var next: ListNode?
    public init(_ val: Int) {
        self.val = val
        self.next = nil
    }
}

下面是核心代码,已在leetcode上成功运行.

func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
    var result: ListNode? = nil
    if l1 == nil && l2 == nil {
        return result
    }
    
    var s1 = l1
    var s2 = l2
    var total = (s1?.val ?? 0) + (s2?.val ?? 0)
    if total < 10 {
        s1 = s1?.next
    } else {
        total = total % 10
        s1 = s1?.next ?? ListNode.init(0)
        s1!.val = s1!.val + 1
    }
    
    result = ListNode.init(total)
    s2 = s2?.next
    result!.next = addTwoNumbers(s1, s2)
    return result
}

 

以上就是别致的两数之和,关于自己解决此类方法的思想和代码,希望对大家有所帮助!!!

Guess you like

Origin www.cnblogs.com/guohai-stronger/p/11715023.html