标签:definition reverse for list 分析
Reverse a singly linked list.Reverse a singly linked list.
Hint:
A linked list can be reversed either iteratively or recursively. Could you implement both?
分析:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* pre = NULL;
if(head == NULL || head->next==NULL) return head;
pre= head->next;
head->next = NULL;
while(pre!=NULL){
ListNode * nextNode = NULL;
nextNode = pre->next;
pre->next = head;
head = pre;
pre = nextNode;
delete nextNode;
}
delete pre;
return head;
}
};
Leetcode[206]-Reverse Linked List
标签:definition reverse for list 分析
原文地址:http://blog.csdn.net/dream_angel_z/article/details/46438359