代码如下

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/                
class Solution {                    //链表无头结点
public:
    ListNode* ReverseList(ListNode* pHead) {   
        if(!pHead)        //如果链表为空,直接返回头指针
        return pHead;
        ListNode*p = pHead->next;   
        pHead->next =NULL;          //将第一个结点孤立出来
        while(p)                   //遍历到最后一个结点
        {
            ListNode*tem = p;       //创建临时指针指向p
            p = p->next;            //p往后移动
            tem->next = pHead;      
            pHead = tem;
        }
    return pHead;
    }
};

 

posted on 2022-12-26 16:45  张家口体校毕业班学渣  阅读(31)  评论(0)    收藏  举报