题目描述

输入一个链表,反转链表后,输出新链表的表头。
C++实现:
思路:头插法实现链表原地逆置
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
        ListNode * linkList = new ListNode(0);
        
        ListNode * p = pHead;
        ListNode * nextP = p->next;
        
        while(p){
            p->next = linkList->next;
            linkList->next = p;
            p = nextP;
            nextP = nextP->next;
        }
        
        return linkList->next;
    }
};

 

 

posted on 2020-07-10 21:32  曹婷婷  阅读(138)  评论(0编辑  收藏  举报