本文共 429 字,大约阅读时间需要 1 分钟。
输入一个链表,反转链表后,输出新链表的表头。
struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { };class Solution {public: ListNode* ReverseList(ListNode* pHead) { ListNode *res = NULL; ListNode *q = pHead; while(q != NULL) { q = q->next; pHead->next = res; res = pHead; pHead = q; } return res; }};
转载地址:http://lixgj.baihongyu.com/