I am working on problem 2 in leetcode (Two Sum) and I keep getting this error. I don't understand how this reflects what I wrote: here is my code that I wrote, I think it has something to do with the dynamic allocations. any help would be appreciated.
/**
* 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;
}
CodePudding user response:
If not take into account typos as for example in this statement
if (iteration = 1){
where there is used the assignment operator =
instead of the comparison operator ==
the program contains serious bugs.
In this code snippet
if (count1 < count2){
int* tmp = l1;
l1 = l2;
l2 = tmp;
int tmpcount = count1;
count1 = count2;
count2 = tmpcount;
}
you are assigning a pointer of the type int *
to a pointer of the type ListNode *
l2 = tmp;
So the compiler should issue a message that there are used pointers of incompatible types.
Also the function can produce a memory leak if empty lists are passed and can be a reason of undefined behavior because for example the last node can be leaved unitialized.
newVal->next = malloc(sizeof(struct ListNode));
newVal = newVal->next;
And if the second list is empty then the pointer start
will not be even itialized.
The second while loop ignores the value of carryFlag
.
In any case the approach is inefficient. There is no any need to count nodes in the two lists.
The function can be declared and defined the following way
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;
}
CodePudding user response:
Variation on @Vlad from Moscow suggested code:
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;
}