我有下面的短代碼,這是一個反轉鏈表的問題的解決方案。單向鏈表的遞歸反轉
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