我对以下一段代码有几个问题。请多多包涵。代码可能很容易理解,但我仍处于学习过程中,所以这对我来说仍然是抽象的。
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;
}
答案 0 :(得分:3)
你写的所有评论都是真的,最后你可以将任何一点视为head
,因为它现在是一个循环列表
线性链表与圆表之间的唯一区别是,第一个案例中最后一个节点指向NULL,或者第二个节点指向第一个节点。
算法:
1)你拿一个temp
指针找到最后一个节点(用J
初始化它,头部,并解析列表,直到你点击NULL)
2)您将temp
(现在是最后一个节点)指向第一个节点,即J