Reverse a singly linked list.
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
ListNode* pre = head;
ListNode* cur = head->next;
ListNode* next = head->next->next;
pre->next = NULL;
while(cur)
{
next = cur->next;
cur->next = pre;
pre = cur;
cur = next;
}
return pre;
}
};
网友评论