码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode] Reverse Linked List

时间:2015-05-05 11:53:38      阅读:115      评论:0      收藏:0      [点我收藏+]

标签:

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

 

[leetcode] Reverse Linked List

标签:

原文地址:http://www.cnblogs.com/lxd2502/p/4478492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!