I am creating two new lists with C, the code is shown below
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode* createList(int list[], int listsize){
struct ListNode *newlistnode;
struct ListNode *curlistnode;
struct ListNode *listhead;
for(int i = 0; i < listsize; i++){
newlistnode = (struct ListNode*) malloc(sizeof(struct ListNode));
newlistnode -> val = list[i];
newlistnode -> next = NULL;
if(listhead == NULL)
listhead = newlistnode;
else
curlistnode ->next = newlistnode;
curlistnode = newlistnode;
}
return listhead;
}
int main(){
int a[4] = {2, 4, 3};
int b[4] = {5, 6, 4};
int listsize = 3;
struct ListNode* list_a = createList(a, listsize);
struct ListNode* list_b = createList(b, listsize);
return 0;
}
here is what I got:
list_a : 2->4->3->NULL
list_b: 2->4->3->5->6->4->NULL
I am confused, can anyone help me?