反转链表

 

 

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) :
            val(x), next(NULL) {
    }
};*/
class Solution {
public:
    ListNode* ReverseList(ListNode* pHead) {
      ListNode* cur=pHead;
      ListNode* pre=nullptr;
      ListNode* nex=nullptr;
      while(cur){
          nex=cur->next;
          cur->next=pre;
          pre=cur;
          cur=nex;
      }
      return pre;
    }
};

 

posted on 2020-12-16 10:36  Taurus20000519  阅读(39)  评论(0)    收藏  举报