剑指offer____反转链表

输入一个链表,反转链表后,输出新链表的表头。
 


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;
    }
};

猜你喜欢

转载自blog.csdn.net/ox0080/article/details/83931765
今日推荐