面试题24. 反转链表

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
           ListNode *pHead = nullptr;
        
        while (head != nullptr)
        {
            ListNode *t = new ListNode(head->val);
            t->next = pHead;
            pHead = t;

            head = head->next;
        }

        return pHead;
    }
};

面试题24. 反转链表

posted @ 2020-02-22 21:26  douzujun  阅读(118)  评论(0编辑  收藏  举报