Suppose you reach last node..
Now inside loop , you increment walker
..so now walker = NULL
..
so this statement gives an error temp->data = walker->data
..
Also you are just creating nodes and copying data not connecting your new linked list..
- You need to maintain new Head pointer to return at the end
- Keep reference to previous node so that you can link it to current node
- Update the pointers
Change it along the lines of this..
struct node* copyList() {
struct node* walker = head; // starting point to "walk" the list
struct node* newHead=NULL,temp,prev=NULL;
while( walker != NULL ){ // done when we reach the node pointing to NULL
temp = (struct node*)malloc(sizeof(struct node)); //create new node
temp->data = walker->data; //copy data
if(prev==NULL) //if its first node
newHead = temp; //new head pointer
else
prev->next = temp; //else link to previous node
prev = temp; //update pointers
walker = walker->next;
}
return newHead;
}