1

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;
} 
1
  • Your function should retutn a pointer. It is defined to return a node, and in practice, it returns nothing. Commented Jul 10, 2019 at 20:14

1 Answer 1

4

You won't have to iterate over the list at all if you do it backwards:

struct ListNode* populateLinkedList(int arr[], int arraysize) {
    struct ListNode* head = NULL;
    for (int i = arraysize; i > 0; i--) {
        struct ListNode* tempNodePtr = (struct ListNode*) malloc(sizeof(*tempNodePtr));
        tempNodePtr->data = arr[i - 1];
        tempNodePtr->next = head;
        head = tempNodePtr;
    }
    return head;
}

As you can see this is a lot simpler because there are no checks since you always replace the head and there's no need to iterate over already inserted elements so it's also more efficient.


As for what's wrong with your solution:

struct ListNode* temp = *node->next;
//                      ^~~~~~~~~~~
// you definitely shouldn't dereferrence anything here

// This condition is wrong because when you exit the loop "temp" will be NULL
while (temp!=NULL) {
    temp = temp->next;
}
temp->next = tempNodePtr;
node->next = temp; // <-- this is definitely not needed

So your code should've been something like this:

struct ListNode* temp = node;
while (temp->next != NULL) {
    temp = temp->next;
}
temp->next = tempNodePtr;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I like the creative solution. Also, could you also pinpoint what went wrong in my code? It's probably around the time where I declare ListNode temp but I can't figure out how to fix it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.