研发埠
标题:
[Microsoft面试]用一种算法来颠倒一个链接表的顺序,如何操作?
[打印本页]
作者:
王鹏
时间:
2013-8-16 10:38
标题:
[Microsoft面试]用一种算法来颠倒一个链接表的顺序,如何操作?
现在在不用递归式的情况下做一遍
作者:
陈荣莲
时间:
2013-8-16 10:46
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;}
欢迎光临 研发埠 (http://bbs.yanfabu.com/)
Powered by Discuz! X3.2