206. Reverse Linked List (List)

Reverse a singly linked list.

/**
* 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) {
if(!head) return head; ListNode* preHead = head;
ListNode* current = preHead->next;
ListNode* curNext;
head->next = NULL; while(current){
curNext = current->next;
current->next = preHead;
preHead = current;
current = curNext;
}
return preHead;
}
};
上一篇:Linux内核中的有关Page的算法


下一篇:HDU3507 Print Article (斜率优化DP基础复习)