1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 static int wing=[]() 10 { 11 std::ios::sync_with_stdio(false); 12 cin.tie(NULL); 13 return 0; 14 }(); 15 16 class Solution 17 { 18 public: 19 ListNode* reverseList(ListNode* head) 20 { 21 if(head==NULL) 22 return NULL; 23 ListNode *pre=NULL,*fon=NULL; 24 while(head!=NULL) 25 { 26 fon=head->next; 27 head->next=pre; 28 pre=head; 29 head=fon; 30 } 31 return pre; 32 } 33 };
头插法,将原链表节点拆下来,用头插法新建链表,达到逆置目的。
 
                    
                 
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号