首页 > 技术文章 > 剑指offer-反转链表

cutelife 2020-09-17 12:34 原文

题目地址:https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&&tqId=11168&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) :
 6             val(x), next(NULL) {
 7     }
 8 };*/
 9 class Solution {
10 public:
11     ListNode* ReverseList(ListNode* pHead) {
12         ListNode* newpHead=NULL;
13         while(pHead!=NULL){
14             ListNode* temp=pHead->next;
15             pHead->next=newpHead;
16             newpHead=pHead;
17             pHead=temp;
18         }
19         return newpHead;
20     }
21 };

 

推荐阅读