I am constructing some code that would basically take in an arrray, and return a linked list in the same order. I'm stuck in a conditional with no way out. How to attach the temp to node? I know I have to traverse by ->next till its not null but I can't figure out how.
#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
struct ListNode{
int data;
struct ListNode* next;
};
struct ListNode populateLinkedList(int arr[], int arraysize){
struct ListNode* head = NULL;
struct ListNode* lastNodePtr = NULL;
struct ListNode* node = NULL;
for(int i=0; i<arraysize; i++){
struct ListNode* tempNodePtr = (struct ListNode*) malloc(sizeof(struct ListNode));
tempNodePtr->data = arr[i];
tempNodePtr->next = NULL;
//if header is empty assign new node to header
if(head==NULL) {
head = tempNodePtr;
}
//if the temp node is empty assign new node to temp node
else if(node==NULL) {
node = tempNodePtr;
}
//if both header and temp node are not empty, attach the temp to node. This is where I get an error.
else {
struct ListNode* temp = *node->next;
while (temp!=NULL){
temp = temp->next;
}
temp->next = tempNodePtr;
node->next = temp;
}
}
//connect head with nodes after index 0
head->next = node;
return head
}
int main() {
printf("Entering program 2\n");
int array[] = {5,8,2,4,12,97,25,66};
int arraysize = (int)( sizeof(array) / sizeof(array[0]));
printf("mainSize: %d \n", arraysize);
populateLinkedList(array, arraysize);
return 0;
}