剑指offer(十五):反转链表

题目描述

输入一个链表,反转链表后,输出新链表的表头。
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;
    }
};

原文地址:https://www.cnblogs.com/ttzz/p/13281308.html