[leetcode] Reverse Linked List
Reverse Linked List
Reverse a singly linked list.
Have you met this question in a real interview?
class Solution { public: ListNode* reverseList(ListNode *head) { if(head == NULL || head->next == NULL) return head; else { ListNode *cur = head, *next_ptr = head->next; head->next = NULL; while(next_ptr) { cur = next_ptr; next_ptr = cur->next; cur->next = head; head = cur; } } return head; } };