f********e 发帖数: 166 | 1 板上大牛能不能给贴个代码啊,反转链表用递归咋写啊?谢谢啊谢谢 |
h******c 发帖数: 75 | 2 // input is the head of the list
// return the new head of reverse list
List* reverse(List* head)
{
if(!head || !head->next)
return head;
List* t = head->next;
head->next = NULL;
List* nhead = reverse(t);
t->next = head;
return nhead;
}
【在 f********e 的大作中提到】 : 板上大牛能不能给贴个代码啊,反转链表用递归咋写啊?谢谢啊谢谢
|
e***s 发帖数: 799 | 3 帅, 不知道 head->next = NULL; 去掉有没有影响?
【在 h******c 的大作中提到】 : // input is the head of the list : // return the new head of reverse list : List* reverse(List* head) : { : if(!head || !head->next) : return head; : List* t = head->next; : head->next = NULL; : List* nhead = reverse(t); : t->next = head;
|
d**e 发帖数: 6098 | 4 if it's removed, the next of the last node in the new list is not null, but
the previous node instead.
【在 e***s 的大作中提到】 : 帅, 不知道 head->next = NULL; 去掉有没有影响?
|
f*******t 发帖数: 7549 | 5 有的,原链表的head的next指针需要通过这条语句设成NULL,中间的node倒无所谓。
【在 e***s 的大作中提到】 : 帅, 不知道 head->next = NULL; 去掉有没有影响?
|