我有下面的短代码,这是一个反转链表的问题的解决方案。单向链表的递归反转
void backwardslist(atom** head) {
atom* first;
atom* second;
if (*head == NULL) return; //if list is empty
first = *head;
second = first->next; // intuitive
if (second == NULL) return;
backwardslist(&second); // recursive call with 2nd one as head, after we got variables
first and second
first->next->next = first; // when we get to the end, we rearrange it
first->next = NULL; // so last one is pointing to first, first is pointing to NULL
*head = second; // I dont understand this part, so the head is changing from the last,
to the second element as the recursion goes to the beginning or am i
missing something?
}
不是第二=(指向两个指针在递归的第二)?
所以第一次,我明白了,它应该指向最后一个,
但随着递归构建背部,其不断变化的*头第二。
第二个atm正在使用什么?
谢谢你们
递归的原则是: 为空表,用1元 -The第一个元素成为最后 列表 - 它的工作原理-I在列表的其余部分调用我的函数 – jambono