Illustrates the algorithm: the check lists as adder

Q: Given two non-empty linked list to represent the two non-negative integer. Wherein, in accordance with their respective bits in reverse stored manner, and each node can store only one digit . Add up the two lists, returns a new list, said they sum.

For example: 342 + 465 = 807

This question of adding two numbers, is the most simple mathematical addition process, but it is built on top of the list, so the difficulty lies in the processing of the list.

Adding, in addition to every addition, the carry bit also need to consider the situation. For this question, every node stores a list of one digit, and is based on the natural numbers stored in reverse, i.e. the end of the chain link head is kept low to high sequence, thus equal to the carry direction and single chain a consistent direction.

Since the characteristics of the single linked list, no predecessor nodes, can not go back. In the scenario of this question, it is only once a while loop, the first chain (lower) end of the chain up to the process (high), can be resolved. However, note that the handling of the carry, each node after the calculation, required by the modulo number 10, store, and more needs to carry to the next node involved in computing, which is consistent with roadmap just a single list.

Then we need several variables, for recording into a carry bit after each operation, but also a dummy node, the node list for recording two lists after addition.

When we deal with the last node longest list, you also need to carry (carry) additional processing, if the carry is not 0, expressed continued Significant bit, the need for additional creating a new node to store a carry.

Here to explain clearly, and directly on the code.

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
  // 计算结果存储的 dummy 结点
  ListNode dummy = new ListNode(0);
  ListNode p = l1, q = l2, curr = dummy;
  // 进位默认为 0
  int carry = 0;
  // 进入循环,以p和q两个链表指针都走到头为结束
  while (p != null || q != null) {
    int x = (p != null) ? p.val : 0;
    int y = (q != null) ? q.val : 0;
    // 进位参与运算
    int sum = carry + x + y;
    // 计算进位
    carry = sum / 10;
    // 构造新的结点存储计算后的位数数值
    curr.next = new ListNode(sum % 10);
    curr = curr.next;
    if (p != null) 
      p = p.next;
    if (q != null) 
      q = q.next;
  }
  // 处理数字最高位末尾进位情况
  if (carry > 0) {
    curr.next = new ListNode(carry);
  }
  return dummy.next;
}

Where p and q are stored by the nodes l1 and l2 of the two lists, as a cyclic basis. Cycling out of the conditions for the two lists have come to the end.

Each cycle, each processing node number and the numerical value obtained by adding, after the calculation of the value modulo carry bit into the new node is stored, and stores the new stored into the carry bits.

One final note, when two lists are processed, but also need to determining whether the most significant bit carry (carry> 0). If necessary, create a new list of storage nodes carry value.

这道利用链表做加法运算的题,就讲解到这里,但是它还有一些变种题。

假如链表不是逆序按位存储数字呢?如果是正序存储。

例如:

1 → 2 → 3

+3 → 2 → 1

=> 123 + 321 = ?

那么如何计算呢?

本文对你有帮助吗?留言、转发、收藏是最大的支持,谢谢!


公众号后台回复成长『成长』,将会得到我准备的学习资料。

Guess you like

Origin www.cnblogs.com/plokmju/p/linked_add.html