代码如下
/* 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; } };
浙公网安备 33010602011771号