📝
notes
  • Initial page
  • 02_ds_algo
    • ds
      • Union Find
      • Binary Indexed Tree
      • Stack
      • String
      • Linked List
      • Segment Tree
      • Union Find
      • Union Find
      • Array
      • Tree
      • Hash Table
      • queue
    • algo
      • Backtracking
      • Sort
      • Binary Search
      • Depth First Search
      • Bit Manipulation
      • Dynamic Programming
      • Breadth First Search
      • Two Pointers
      • Math
      • Sliding Window
    • leetcode
      • List
      • 1. Two Sum
      • READEME
      • 2. Add Two Numbers
  • README
    • README
      • pointer
      • effective-cpp
      • roadmap
      • pimpl
      • smartptr
  • 03_cheatsheet
    • README
      • git
      • gdb
    • README
      • bash
      • Python 速查表中文版
    • README
      • vim
Powered by GitBook
On this page
  • 题目
  • 题目大意
  • 解题思路
  • 代码

Was this helpful?

  1. 02_ds_algo
  2. leetcode

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.

题目大意

2 个逆序的链表,要求从低位开始相加,得出结果也逆序输出,返回值是逆序结果链表的头结点。

解题思路

需要注意的是各种进位问题。

极端情况,例如

Input: (9 -> 9 -> 9 -> 9 -> 9) + (1 -> )
Output: 0 -> 0 -> 0 -> 0 -> 0 -> 1

为了处理方法统一,可以先建立一个虚拟头结点,这个虚拟头结点的 Next 指向真正的 head,这样 head 不需要单独处理,直接 while 循环即可。另外判断循环终止的条件不用是 p->next != null,这样最后一位还需要额外计算,循环终止条件应该是 p != nil。

代码

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
 public:
  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode dummy_head;
    ListNode* p = &dummy_head;
    ListNode* p1 = l1;
    ListNode* p2 = l2;
    int sum;
    int carry = 0;
    while (p1 != nullptr && p2 != nullptr) {
      sum = p1->val + p2->val + carry;      
      carry = sum / 10;
      p1 = p1->next;
      p2 = p2->next;
      p->next = new ListNode(sum % 10);
      p = p->next;
    }
    while (p1 != nullptr) {
      sum = p1->val + carry;
      p1 = p1->next;
      carry = sum / 10;
      p->next = new ListNode(sum % 10);
      p = p->next;
    }
    while (p2 != nullptr) {
      sum = p2->val + carry;
      p2 = p2->next;
      carry = sum / 10;
      p->next = new ListNode(sum % 10);
      p = p->next;
    }
    if (carry) {
      p->next = new ListNode(carry);
      p = p->next;
    }
    return dummy_head.next;
  }
};

// 将条件移到循环内部
class Solution2 {
 public:
  ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode dummy_head;
    ListNode* p = &dummy_head;
    ListNode* p1 = l1;
    ListNode* p2 = l2;

    int n1;
    int n2;
    int sum;
    int carry = 0;
    while (p1 || p2 || carry) {      
      if (p1) {
        n1 = p1->val;
        p1 = p1->next;
      } else {
        n1 = 0;
      }
      if (p2) {
        n2 = p2->val;
        p2 = p2->next;
      } else {
        n2 = 0;
      }
      sum = n1 + n2 + carry;      
      carry = sum / 10;      
      p->next = new ListNode(sum % 10);
      p = p->next;
    }
    return dummy_head.next;
  }
};
PreviousREADEMENextREADME

Last updated 4 years ago

Was this helpful?