【发布时间】:2021-12-31 10:59:44
【问题描述】:
我想对节点求和,直到达到 0 并用新值更新原始链表。
注意:它会跳过 0,直到到达要计算总和的数字或链表的末尾。
节点定义:
struct Node {
int data;
Node* next;
};
void updateLinkedList(Node* head)
{
Node* currentNode = head;
int temp = 0;
int sum = 0;
while (currentNode != NULL)
{
temp = currentNode->data;
while(temp != 0)
{
sum = sum + currentNode->data;
currentNode = currentNode->next;
}
}
}
我正在尝试做下一件事:
用户输入链表:
1 2 0 5 3 0 4
更新的链表:
3 8 4
【问题讨论】:
-
如果列表以 0 开头,例如 0 1 2 会怎样?
-
@VladfromMoscow 如果没有数字,它将跳过 0 直到到达数字或列表末尾
-
你能发布
Node的定义吗? -
@LHLaurini 在那里,我添加了它:)
标签: c++ struct linked-list singly-linked-list function-definition