剑指offer--22.反转链表

时间限制:1秒 空间限制:32768K 热度指数:440624
本题知识点: 链表

题目描述

输入一个链表,反转链表后,输出新链表的表头。
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
    public:
        ListNode* ReverseList(ListNode* pHead) {
            if(pHead == NULL) return NULL;
            ListNode* node = NULL,*pNext;
            pNext = pHead->next;//reserve
            pHead->next = NULL;    //断开
            node = pHead;//start
            pHead = pNext;
                        
            while(pHead != NULL) {
                pNext = pHead->next;//reserve
                pHead->next = NULL;    //断开
                
                pHead->next = node;//反转
                node = pHead; 
                
                pHead = pNext;//接着 
            }
            return node;
        }
};

 

posted @ 2019-03-29 10:45  朤尧  阅读(246)  评论(0编辑  收藏  举报