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.
|