[Microsoft面试]用一种算法来颠倒一个链接表的顺序,如何操作?
现在在不用递归式的情况下做一遍 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]