将两个链接列表相加,其中每个节点都是一个数字

时间:2012-11-01 23:40:20

标签: c++ linked-list

我正在尝试编写一个求和的函数,每个函数都由一个链表表示,其中每个节点 - >数据是一个数字0-9。最低位数位于列表的首位,最多位于尾部。

这来自Cracking the Coding Interview。这是我的代码:

SinglyLinked<int>& addLists(SinglyLinked<int>& ll1, SinglyLinked<int>& ll2)
{
  SinglyLinked<int> *sumList = new SinglyLinked<int>;

  ListNode<int> *node1 = ll1.head; ListNode<int> *node2 = ll2.head;

  int sum, carry = 0;
  while(node1 || node2)
  {
    if(node1)
      sum += node1->data;
    if(node2)
      sum += node2->data;
    sum += carry;

    carry = 0;
    sumList->insertAtEnd(sum%10);
    if(sum > 9)
      carry = 1;
    sum = 0;
    node1 =  node1->next; node2 = node2->next;
  }
  return *sumList;
}

首先,这段代码是否正确?它似乎有效,但是当给出两个不同长度的链表(整数)时它会出错。我想知道问题是否只是为了解决整数长度相同的情况。如果没有,我将如何总结两个不同长度的列表?我天真的解决方案是存储每个列表的长度,然后使用它来创建只有一个数字将贡献的数字,直到两个整数对齐。还有比这更优雅的东西吗?

4 个答案:

答案 0 :(得分:1)

它会在不同长度的列表中进行段错误,因为node1node2指向空。

更改

  

node1 = node1-&gt; next; node2 = node2-&gt; next;

if (node1)
    node1 = node1->next;

if (node2)
    node2 = node2->next;

答案 1 :(得分:0)

while(node1 || node2)

如果任何一个节点都可以继续。但该块预计两个节点在到达时都是有效的:

node1 =  node1->next; node2 = node2->next;

你需要在if node1中检查你的“nexts”:

if(node1) {
  sum += node1->data;
  node1 = node1->next;
}

答案 2 :(得分:0)

有一个条件,其中node1和node2将指向NULL,如果前一个数字操作有进位,则不会将其附加到总和列表的末尾。例如,6-> 5-> 4 + 4-> 5-> 6应该是0-> 1-> 1-> 1但是总和将是0-> 1-> 1 。所以在返回行之前你应该添加:

if (carry)
   sumList->insertAtEnd(carry);

答案 3 :(得分:0)

此问题的另一个解决方案是在每个列表中添加数字时,将它们一起添加以获得等于两个列表总和的大和,将该总和转换为字符串,并将字符串的每个字符附加到a新名单。希望它可以帮助某人。以下是代码。

node.h文件

#ifndef node_h
#define node_h

class LinkedList
{

private:
    struct node
    {
        int data;
        node *next;
    };
    node *head;

public:
    LinkedList ();
    node *createNode (int key); 
    void appendNodeBack (const int &key);
    void printList ();
    int SumOfNodes (const LinkedList list1);
};
#endif

node.cpp文件

#include "node.h"
#include <math.h>

LinkedList::LinkedList ():head(NULL) {}

LinkedList::node *LinkedList::createNode (int key)
{
    node *newNode = new node;
    newNode->data = key;
    newNode->next = NULL;
    return newNode;
}

void LinkedList::appendNodeBack (const int &key)
{
    node *newNode = createNode (key);

    //if tree is empty
    if (head == NULL)
    {
        head = newNode;
        return;
    }

    //if tree is not empty
    //traverse to the last node in the list
    node *temp = head;
    while (temp->next != NULL)
        temp = temp->next;
    temp->next = newNode;
}

void LinkedList::printList ()
{
    //if tree is empty
    if (head == NULL)
    {
        std::cout << "Tree is empty!\n";
        return;
    }

    //if tree not empty
    node *temp = head;
    while (temp != NULL)
    {
        std::cout << temp->data<<"-->";
        temp = temp->next;

    }
    std::cout << "NULL\n";
}

int LinkedList::SumOfNodes (const LinkedList list1)
{   
    //find the length of the list
    node *temp = head;
    int count = 0;

    while (temp != NULL)
    {
        count++;
        temp = temp->next;
    }

    //re-assign temp to head
    temp = head;

    //calculate the sum
    unsigned int sum = 0;

    for (unsigned int i = 1; i < pow (10, count); i = i * 10)
    {
        sum = sum + temp->data * i;
        temp = temp->next;
    }

    return sum;
}

main.cpp文件

#include <iostream>
#include "node.cpp"

int main ()
{
    LinkedList list1, list2, list3;
    list1.appendNodeBack (2);
    list1.appendNodeBack (3);
    list1.appendNodeBack (5);
    list1.appendNodeBack (4);

    list2.appendNodeBack (5);
    list2.appendNodeBack (6);
    list2.appendNodeBack (7);
    list2.appendNodeBack (8);

    list1.printList ();
    std::cout << list1.SumOfNodes (list1) << std::endl;

    list2.printList ();
    std::cout << list2.SumOfNodes (list2) << std::endl;

    unsigned int sum = list1.SumOfNodes (list1) + list2.SumOfNodes (list2);

    //convert the number to string
    std::string str = std::to_string (sum);

    //append integer value to the new list
    for (unsigned int i = 0; i < str.length (); i++)
        list3.appendNodeBack (int (str[i] - '0'));

    std::cout << "The new list becomes\n";
    list3.printList();

    return 0;
}