原文链接:点击打开链接
Reverse a singly linked list
A linked
list can be reversed either iteratively or recursively. Could you implement both?
struct ListNode* reverseList(struct ListNode* head) { // 链表为空或者只有头结点 if (!head || !head->next) return head; struct ListNode* p1 = head, *p2 = head->next, *p3 = p2; while (NULL != p2) { p3 = p2->next; p2->next = p1; p1 = p2; p2 = p3; } head->next = NULL; return (head = p1); }