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. 查看Centos系统信息命令

    linux命令行具有强大的功能,我们安装vps后,首先应该知道系统信息,查看这些信息,你会发现Linux命令很简单,你可以按照下面的命令练习. linux系统信息 # uname -a # 查看内核/ ...

  2. 说说php取余运算(%)的那点事

    http://www.phpddt.com/php/php-take-over.html       fmod()与 % 区别   都是取余 fmod是函数 原型float fmod(float x, ...

  3. JAVA float double数据类型保留2位小数点5种方法

    /** * Java 两个整数相除保留两位小数,将小数转化为百分数 * java中,当两个整数相除时,由于小数点以后的数字会被截断,运算结果将为整数,此时若希望得到运算结果为浮点数,必须将两整数其一或 ...

  4. linux gcc loudong

    五事九思 (大连Linux主机维护) 大连linux维护qq群:287800525 首页 日志 相册 音乐 收藏 博友 关于我     日志       spcark_0.0.3_i386.src.t ...

  5. Web前端名词释义及原理

    引言:看题目的时候,不要觉得这是一个很深奥的问题,Web前端这些东西很多就是叫的名字牛逼,其实原理很TM简单,也就那么回事. 一.javascript名词释义 1.啥是事件队列? 就是 弄一个数组,里 ...

  6. Set Matrix Zeroes

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...

  7. 机器学习之神经网络模型-上(Neural Networks: Representation)

    在这篇文章中,我们一起来讨论一种叫作"神经网络"(Neural Network)的机器学习算法,这也是我硕士阶段的研究方向.我们将首先讨论神经网络的表层结构,在之后再具体讨论神经网 ...

  8. Windows PAE 寻址

    PAE 就是物理地址扩展.我们常规的寻址方式是之前的将虚拟地址化为10 10 12的方式来寻址页目录,页表,页偏移,但是在开始PAE之后的寻址方式发生了改变,将32位的虚拟地址转化成 2 9 9 12 ...

  9. Windows 回调监控 <二>

    在之前的文章Windows 回调监控 <一> 总结了关于CreateProcessNotify,CreateProcessNotifyEx和LoadImageNotify一些用法,之后产生 ...

  10. 545D. Queue

    http://codeforces.com/problemset/problem/545/D 题意:n个数的服务请求数组,求在其服务时间内,最大的可满足服务的请求数量 首先对服务请求数组按照从小到大排 ...