王鹏 发表于 2013-8-16 10:38:36

[Microsoft面试]用一种算法来颠倒一个链接表的顺序,如何操作?

现在在不用递归式的情况下做一遍

陈荣莲 发表于 2013-8-16 10:46:00

Node *reverse(Node * head) {if (head == NULL) return head;if (head->next == NULL) return head;Node * ph = reverse(head->next);head->next->next = head;head->next = NULL;return ph;}Node * reverseNonrecurisve(Node * head) {if (head == NULL) return head;Node * p = head;Node * previous = NULL;while (p->next != NULL) {    p->next = previous;    previous = p;    p = p->next;}p->next = previous;return p;}
页: [1]
查看完整版本: [Microsoft面试]用一种算法来颠倒一个链接表的顺序,如何操作?