只用了迭代,等会看一下大神的递归解法;
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 class Solution { 10 public: 11 ListNode* reverseList(ListNode* head) { 12 if(head==NULL||head->next==NULL) return head; 13 ListNode* pre,*cur; 14 pre=head;cur=head->next; 15 head->next=NULL; 16 while(cur!=NULL){ 17 ListNode* temp; 18 temp=cur->next; 19 cur->next=pre; 20 pre=cur; 21 cur=temp; 22 } 23 return pre; 24 } 25 };
leetcode 206 反转链表 Reverse Linked List
原文:https://www.cnblogs.com/joelwang/p/10447288.html