[leetcode] Reverse Linked List

Reverse Linked List

 

Reverse a singly linked list.

click to show more hints.

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;
  }
};

 

posted @ 2015-05-05 10:36  imKirin  阅读(126)  评论(0编辑  收藏  举报