题目描述:
Reverse a singly linked list.
翻转一个单向链表, 就是从前往后依次翻转即可.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
class Solution { public: ListNode* reverseList(ListNode* head) { if(!head) return NULL; ListNode *p1 = head, *p2 = head->next, *p = head->next; if(!p1 || !p2) return head; while(p2 && p1){ ListNode *t = p2->next; p2->next = p1; p1 = p2; p2 = t; } head->next = NULL; return p1; } };
|