NC78 反转链表 牛客

描述

输入一个链表,反转链表后,输出新链表的表头。

示例1

输入:
{1,2,3}
返回值:
{3,2,1}
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
      ListNode *pre=nullptr;
      ListNode *cur=pHead;
      ListNode *nxt=nullptr;
      while(cur)
      {
          nxt=cur->next;
          cur->next=pre;
          pre=cur;
          cur=nxt;
      }
        return pre;
    }
};
原文地址:https://www.cnblogs.com/stepping/p/14838746.html