【发布时间】:2020-02-17 20:31:53
【问题描述】:
struct node* question4(struct node *list){ //list = 66, 9, 14, 52, 87, 14, 17
struct node* a = list; //a = 66, 9, 14, 52, 87, 14, 17 pointing to head (66)
struct node* b = list; //b = 66, 9, 14, 52, 87, 14, 17 pointing to head (66)
struct node* c;
if(a == NULL) return NULL; // a is not NULL it's pointing to 66
while(a->next != NULL) // run until a points to the last element (17)
a = a->next;
a->next = b; //next element of 17 points to b (which is 66).
c = b->next; // c points to what b is pointing to next which is 9.
b -> next = NULL; // next element of b is NULL(instead of 14). what happens here?
return c;
}
所以a连接到b。所以a的元素是这样的? 66->9->14->52->87->14->17->66->9->14->52->87->14->17
而 b 只是 66->9->NULL?
c 是 9->14->52->87->14->17?或 9->14->52->87->14->17->66 ?为什么?
我目前正在学习链表,谢谢大家的帮助!
【问题讨论】:
-
b->next基本上是通过用NULL替换该指针来破坏原始链表。然后该函数返回列表的剩余部分。
标签: c linked-list rotation singly-linked-list