由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - reverse LL recursively
相关主题
树的前序遍历const_reverse_iterator和reverse_iterator有什么区别? (转载)
std::list::iterator questionPython: What does this mean?
scoped lock的问题recurvion真的很难懂~~
为什么会有recursion stack overflow这个问题?reverse words, not the Microsoft one!!!
ECMAScript 6将迎来tail recursionReversing a singly linked list
如何编程实现循环嵌套的次数?考考你的能力。
再问两个C++问题Reverse Words in a String
[合集] 问个递归的问题有没有大牛说说C里边for循环的坏处
相关话题的讨论汇总
话题: head话题: tail话题: ll话题: reverse
进入Programming版参与讨论
1 (共1页)
c**m
发帖数: 30
1
One of my co-workers once was very impressed by someone he interviewed
because that person reversed linked-list recursively.
I never thought of this as a recursion problem.. Actually I am not sure how
to do it recursively. Anyone care to pitch in?
p***o
发帖数: 1252
2
I guess most such kind of problems come from LISP.

how

【在 c**m 的大作中提到】
: One of my co-workers once was very impressed by someone he interviewed
: because that person reversed linked-list recursively.
: I never thought of this as a recursion problem.. Actually I am not sure how
: to do it recursively. Anyone care to pitch in?

t****t
发帖数: 6806
3
tail recursion can be transformed to iterations
on the other hand, some iterations can be transformed to tail recursion as
well, i guess
in your case, i guess
reverse(head)=head ? [reverse(head->next), head] : null

how

【在 c**m 的大作中提到】
: One of my co-workers once was very impressed by someone he interviewed
: because that person reversed linked-list recursively.
: I never thought of this as a recursion problem.. Actually I am not sure how
: to do it recursively. Anyone care to pitch in?

l*****d
发帖数: 359
4
void recrevList(NodeType* &head, NodeType* &tail)
{
if (head!=tail) recrevList(head->next, tail);
NodeType* tmp = head;
head = head->next;
tail = tail->next = tmp;
tail->next = 0;
return;
}
c**m
发帖数: 30
5
好象不可以,try a list with only one node.

【在 l*****d 的大作中提到】
: void recrevList(NodeType* &head, NodeType* &tail)
: {
: if (head!=tail) recrevList(head->next, tail);
: NodeType* tmp = head;
: head = head->next;
: tail = tail->next = tmp;
: tail->next = 0;
: return;
: }

l*****d
发帖数: 359
6
right, i should put all the stuff in a block after "if"

【在 c**m 的大作中提到】
: 好象不可以,try a list with only one node.
1 (共1页)
进入Programming版参与讨论
相关主题
有没有大牛说说C里边for循环的坏处ECMAScript 6将迎来tail recursion
Comparison Re: 组合的枚举算法?如何编程实现循环嵌套的次数?
这道题有什么好思路?再问两个C++问题
Interview question[合集] 问个递归的问题
树的前序遍历const_reverse_iterator和reverse_iterator有什么区别? (转载)
std::list::iterator questionPython: What does this mean?
scoped lock的问题recurvion真的很难懂~~
为什么会有recursion stack overflow这个问题?reverse words, not the Microsoft one!!!
相关话题的讨论汇总
话题: head话题: tail话题: ll话题: reverse