leetcode:Reverse Linked List
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的更多相关文章
- 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-> ...
- [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 ...
- [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-> ...
- [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 ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- 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-> ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- 【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 ...
随机推荐
- bzoj 1041 圆上的整点 分类: Brush Mode 2014-11-11 20:15 80人阅读 评论(0) 收藏
这里先只考虑x,y都大于0的情况 如果x^2+y^2=r^2,则(r-x)(r+x)=y*y 令d=gcd(r-x,r+x),r-x=d*u^2,r+x=d*v^2,显然有gcd(u,v)=1且u&l ...
- IAP Store Kit Guide(中文)
IAP Store Kit Guide(中文) 一.In App Purchase概览 Store Kit代表App和App Store之间进行通信.程序将从App Store接收那些你想要提供的产品 ...
- 高德地图根据经纬度转换成地址JS代码demo
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- Sqli-labs less 35
Less-35 35关和33关是大致的一样的,唯一的区别在于sql语句的不同. $sql="SELECT * FROM users WHERE id=$id LIMIT 0,1"; ...
- flume-ng+Kafka+Storm+HDFS 实时系统搭建
转自:http://www.tuicool.com/articles/mMrQnu7 一 直以来都想接触Storm实时计算这块的东西,最近在群里看到上海一哥们罗宝写的Flume+Kafka+Storm ...
- ios开发之AppDelegate
创建应用程序之后之后,默认有AppDelegate.h文件与AppDelegate.m文件. AppDelegate为何物? AppDelegate为整个应用的一个代理,提供程序启动.退出等类似 ...
- light oj 1140 - How Many Zeroes? 数位DP
思路:dp[i][j]:表示第i位数,j表示是否有0. 代码如下: #include<iostream> #include<stdio.h> #include<algor ...
- 如何修改Linux系统的TTL值
在网络中,黑客如果用ping命令去探测 一个主机,根据TTL基数可以推测操作系统的类型.对于一个没有经过任何网关和路由的网络, 直接ping对方系统得到的TTL值,被叫做"TTL基数&qu ...
- 内存分析_.Net垃圾回收介绍
垃圾回收 1. .Net垃圾回收中涉及的名称 1.1.什么是代? 垃圾回收器为了提升性能使用了代的机制,共分为三代(Gen0.Gen1.Gen2).GC工作机制基于以下假设, 1) 对象 ...
- PHP页面跳转几种实现技巧
PHP被许多程序员用来开发WEB的首选语言.在实际开发中,网站的各项功能都可以通过PHP语言的编写来满足,比如PHP页面跳转这一方法. 探讨PHP变量解析顺序如何获取提交数据 深入解读PHP运行机制 ...