我正在处理leetcode (两个求和)中的问题2,并且一直收到这个错误。我不明白这如何反映我所写的内容:这是我编写的代码,我认为它与动态分配有关。任何帮助都将不胜感激。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int GetLength(struct ListNode* list){
int count = 0;
while (list != NULL){
count ++;
list = list->next;
}
return count;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
int count1 = GetLength(l1);
int count2 = GetLength(l2);
struct ListNode* newVal = malloc(sizeof(struct ListNode));
struct ListNode* start;
if (count1 < count2){
int* tmp = l1;
l1 = l2;
l2 = tmp;
int tmpcount = count1;
count1 = count2;
count2 = tmpcount;
}
// updated l1 is longer
int carryflag = 0;
int iteration = 0;
while (l2 != NULL){
iteration ++;
int sum = l1->val + l2->val + carryflag;
printf("%d\n", iteration);
if (iteration = 1){
start = newVal;
}
newVal->val = sum % 10;
if (sum >= 10) carryflag = 1;
else carryflag = 0;
l1 = l1->next;
l2 = l2->next;
newVal->next = malloc(sizeof(struct ListNode));
newVal = newVal->next;
}
while(l1 != NULL){
newVal->next = malloc(sizeof(struct ListNode));
newVal->val = l1->val;
l1 = l1->next;
newVal = newVal->next;
}
return start;
}发布于 2022-08-22 14:28:44
如果不考虑排字,例如在本声明中。
if (iteration = 1){在使用赋值运算符=而不是比较运算符==的地方,程序包含严重的错误。
在这个代码片段中
if (count1 < count2){
int* tmp = l1;
l1 = l2;
l2 = tmp;
int tmpcount = count1;
count1 = count2;
count2 = tmpcount;
}将类型int *的指针分配给ListNode *类型的指针。
l2 = tmp;因此,编译器应该发出一条消息,说明使用了不兼容类型的指针。
此外,如果传递空列表,该函数可能会产生内存泄漏,并且可能是未定义行为的一个原因,因为例如,最后一个节点可以被统一化。
newVal->next = malloc(sizeof(struct ListNode));
newVal = newVal->next;如果第二个列表是空的,那么指针start甚至不会被化。
第二个while循环忽略carryFlag的值。
无论如何,这种方法是低效的。不需要对两个列表中的节点进行计数。
该函数可以通过以下方式声明和定义
struct ListNode * addTwoNumbers( const struct ListNode *l1, const struct ListNode *l2 )
{
const int Base = 10;
struct ListNode *head = NULL;
struct ListNode **current = &head;
int carryFlag = 0;
while ( carryFlag != 0 || l1 != NULL || l2 != NULL )
{
*current = malloc( sizeof( struct ListNode ) );
int value = ( l1 == NULL ? 0 : l1->val ) +
( l2 == NULL ? 0 : l2->val ) +
carryFlag;
( *current )->val = value % Base;
carryFlag = value / Base;
( *current )->next = NULL;
current = &( *current )->next;
if ( l1 != NULL ) l1 = l1->next;
if ( l2 != NULL ) l2 = l2->next;
}
return head;
}发布于 2022-08-22 15:16:23
对@Vlad from Moscow建议代码的更改:
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode * addTwoNumbers(const struct ListNode *l1, const struct ListNode *l2) {
struct ListNode head = {.next = NULL};
struct ListNode *previous = &head;
const int Base = 10;
int carry = 0;
while (l1 || l2 || carry) {
int value = carry;
if (l1) {
value += l1->val;
l1 = l1->next;
}
if (l2) {
value += l2->val;
l2 = l2->next;
}
struct ListNode *current = malloc(sizeof current[0]);
if (current == NULL) {
// TBD code to handle error
return NULL;
}
current->next = NULL;
current->val = value % Base;
carry = value / Base;
previous->next = current;
previous = current;
}
return head.next;
}https://stackoverflow.com/questions/73446259
复制相似问题