Reverse a singly linked list.

代码如下:

the iterative solution:(c++)

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *temp = NULL , *nextNode = NULL;
while(head){
nextNode = head->next; //save the current's next node 
head->next = temp; //let the current point to its previous one
temp = head; //save the current node as pre
head = nextNode; //move to next node
}
return temp; //just point to the last node we wanted
}
};

 或:

class Solution {
public:
ListNode *reverseList(ListNode *head)
{
if (head == NULL || head->next == NULL)
return head; ListNode *pCurr = head;
ListNode *pPrev = NULL;
ListNode *pNext = NULL; while (pCurr != NULL)
{
pNext = pCurr->next; //save next node
pCurr->next = pPrev;
if (pNext == NULL)
head = pCurr;
pPrev = pCurr;
pCurr = pNext;
} return head;
}
};

  

 

其他解法:

1、 the recursive version:(c++)

class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL || head->next == NULL) return head; // case proof and recursion exit conditon
ListNode *np = reverseList(head->next);
head->next->next = head; // make the next node point back to the node itself
head->next = NULL; // destroy the former pointer
return np;
}
};

 2、(c++)

 class Solution {
public:
ListNode* reverseList(ListNode* head) {
stack<ListNode*> s;
ListNode *tail=NULL;
while(head)
{
s.push(head);
head=head->next;
}
if(!s.empty())
head=s.top();
while(!s.empty())
{
tail=s.top();
s.pop();
if(!s.empty())
tail->next=s.top();
else
tail->next=NULL;
}
return head;
}
};

  3、(c)

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head)
{
struct ListNode* last = 0; while (head)
{
struct ListNode* next = head->next;
head->next = last;
last = head;
head = next;
}; return last;
}

 或:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {
if (!head)
return NULL; struct ListNode *curr = head;
struct ListNode *next = NULL;
struct ListNode *prev = NULL; while (curr){
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
head = prev;
} return head;
}

  

 更多:http://blog.chinaunix.net/uid-7471615-id-83821.html

leetcode:Reverse Linked List的更多相关文章

  1. leetcode:Reverse Linked List II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  2. [LeetCode] 92. Reverse Linked List II 倒置链表之二

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  3. [LeetCode] 92. Reverse Linked List II 反向链表II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...

  4. [LeetCode] 92. Reverse Linked List II_Medium tag: Linked List

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  5. LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

    1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...

  6. Java for LeetCode 092 Reverse Linked List II

    Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...

  7. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  8. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  9. 【leetcode】Reverse Linked List II

    Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...

随机推荐

  1. JSP页面批量选择&全选操作&选择回显

    效果如下: js验证部分: 页面body部分: 附:控制器Controller中验证批量选择条件回显:

  2. Leetcode#128 Longest Consecutive Sequence

    原题地址 1. 把所有元素都塞到集合里2. 遍历所有元素,对于每个元素,如果集合里没有,就算了,如果有的话,就向左向右拓展,找到最长的连续范围,同时在每次找的时候都把找到的删掉.这样做保证了同样的连续 ...

  3. WebService流行框架之Axis和CXF

    转自:http://www.cnblogs.com/snake-hand/archive/2013/06/09/3129915.html 前言 上节课我们对WebService进行了简单的介绍,对于其 ...

  4. Swift-1-基本概念

    // Playground - noun: a place where people can play // 通过代码快速了解swift常用知识,需要一定object-c基础 import UIKit ...

  5. [百度空间] [转]内存屏障 - MemoryBarrier

    处理器的乱序和并发执行 目前的高级处理器,为了提高内部逻辑元件的利用率以提高运行速度,通常会采用多指令发射.乱序执行等各种措施.现在普遍使用的一些超标量处理器通常能够在一个指令周期内并发执行多条指令. ...

  6. Unity3D研究院之LZMA压缩文件与解压文件

    原地址:http://www.xuanyusong.com/archives/3095 前两天有朋友告诉我Unity的Assetbundle是LZMA压缩的,刚好今天有时间那么就研究研究LZMA.它是 ...

  7. mysql存储过程和事件

    1.会员表member和车辆表car,更新每个会员下面的车辆数量have_car字段. DELIMITER $$ USE $$ DROP PROCEDURE IF EXISTS `sp_update_ ...

  8. Comet技术浅论

    1.如何实现一个轮询? function getMessage(url,callback){ var XHR=new XMLHttpRequest(); XHR.open('get',url,true ...

  9. HDU4966 GGS-DDU(最小树形图)

    之前几天想着补些算法的知识,学了一下最小树形图的朱刘算法,不是特别理解,备了份模板以备不时之需,想不到多校冷不丁的出了个最小树形图,没看出来只能表示对算法不太理解吧,用模板写了一下,然后就过了.- - ...

  10. POJ 2041

    #include <iostream> #include <string> #include <algorithm> using namespace std; st ...