Reputation: 2529
I have a few questions for the following piece of code. Please bear with me. The code might be easy to understand, but I am still in the process of learning, so this is still abstract to me.
struct listNode {
int data;
struct listNode *next };
//Is J a pointer, pointing to the head of the linked list?
struct listNode * convert ( struct listNode * J) {
if (J == NULL)
return NULL;
//Is this creating a new temporary pointer that will traverse the linked list?
//Is it being set to J so that it can start at the first node and go to the last?
struct listNode * temp = J;
while ( temp -> next != NULL)
temp = temp->next; //Is this where the temp pointer actually goes through the list?
//Temp->next will eventually become the last node of the list and that will be set to J
//which is the head pointer?
temp->next = J;
return temp;
}
Upvotes: 1
Views: 1554
Reputation: 693
Everything you wrote as comments is true, and at the end you can consider any point as head
since it's a circular list now
the only difference between a linear linked list and a circular one is that the last node points to NULL in the first case, or it points to the first node, for the second.
The algorithm:
1) you take a temp
pointer to find the last node (initialise it with J
, the head, and parse the list until you hit NULL)
2) you point temp
, which is the last node now, to the first node, which is J
Upvotes: 3