链表逆序

  1. class Solution {  
  2. public:  
  3.     ListNode* reverseList(ListNode* head) {  
  4.         if(head==NULL)  
  5.             return NULL;  
  6.         ListNode* pre = new ListNode(-1);  
  7.         pre->next = head;  
  8.         ListNode* cur = head;  
  9.         ListNode* nex = head;  
  10.         while(cur->next){  
  11.             nex = cur->next;  
  12.             cur->next = nex->next;  
  13.             nex->next = pre->next;  
  14.             pre->next = nex;  
  15.         }  
  16.         return pre->next;  
  17.     }  
  18. }; 

猜你喜欢

转载自blog.csdn.net/jwy2014/article/details/79514678