boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 反转链表递归怎么写?
相关主题
f 的面经
弱问题,连反转链表都看不懂了
问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5,
java 链表里面dummy node 一问?谢谢
问个题,用递归方法
一到电面题
10分钟前的F家电面面经
一道老题
BST和有序双向链表的相互转换?
问一道常见面试题,reverse a linked list
相关话题的讨论汇总
话题: head话题: list话题: next话题: null话题: 链表
进入JobHunting版参与讨论
1 (共1页)
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; 去掉有没有影响?
1 (共1页)
进入JobHunting版参与讨论
相关主题
问一道常见面试题,reverse a linked list
google phone interview
问了一个链表,1->2->3->4->5, 每两个交换,2->1->4->3->5
leetcode上这个链表节点的定义是什么意思?ListNode(int x) : val(x), next(NULL) {}
n个排序链表,如何O(1) space合并成一个
链表插入排序都写了一个小时,对人生失去信心了。
到底怎么了?很多面试来的居然连reverse一个链表都写不来
两个链表怎么查找相交点?
问个链表反转的老题
反转链表很难写吗???
相关话题的讨论汇总
话题: head话题: list话题: next话题: null话题: 链表