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

LeetCode_Reverse Linked List

时间:2015-05-18 10:59:26      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:c++   算法   algorithm   leetcode   

一.题目

Reverse Linked List

  Total Accepted: 9559 Total Submissions: 28250My Submissions

Reverse a singly linked list.

click to show more hints.

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

Show Tags
Have you met this question in a real interview?  
Yes
 
No

Discuss








二.解题技巧

    这是一道简单的链表反转的题,并不考察什么算法,只是考察链表的基础知识。


三.实现代码


/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/

#include <iostream>

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution
{
public:
    ListNode* reverseList(ListNode* head)
    {
        ListNode* Pre = NULL;
        ListNode* Now = head;
        ListNode* Next = NULL;

        while(Now)
        {
            Next = Now->next;
            Now->next = Pre;
            Pre = Now;
            Now = Next;
        }

        return Pre;

    }
};




四.体会

    这是一道简单的题,基础题。



版权所有,欢迎转载,转载请注明出处,谢谢技术分享
技术分享




LeetCode_Reverse Linked List

标签:c++   算法   algorithm   leetcode   

原文地址:http://blog.csdn.net/sheng_ai/article/details/45815155

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