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 ...
随机推荐
- Leetcode#117 Populating Next Right Pointers in Each Node II
原题地址 二叉树的层次遍历. 对于每一层,依次把各节点连起来即可. 代码: void connect(TreeLinkNode *root) { if (!root) return; queue< ...
- Post 的数据被截断
原因: Form 域 POST 提交数据 100K(可能不是这个值) 限制的解决方案 因为微软这个限制是对表单内每个域(第一个控件)的限制.问题的解决办法是,对于一个需要发送大数据的域,在提交表单 ...
- 将Asp.Net页面输出到EXCEL里去
其实,利用ASP.NET输出指定内容的WORD.EXCEL.TXT.HTM等类型的文档很容易的.主要分为三步来完成. 一.定义文档类型.字符编码 Response.Clear(); Respons ...
- 如何做好一名DBA【转】
我一直有一个观点:程序是暂时的,而数据是永恒的.所以我一直都认为数据的重要性在很多企业中都远远高于应用程序,在多年的工作实践中努力做好DBA的工作.而要做好一名DBA,必须要清楚作为一名DBA的职责. ...
- code::blocks 初使用遇到的问题记录
/* 做本程序遇到的问题:由于使用的是CODE::BLOCKS 开发环境,刚开始使用code::blocks是,什么都 没有设置,居然输入的中文字符串,保存项目后,再次打开,code::blocks不 ...
- CentOS用yum安装X Window
安装X图形界面系统 yum list 列出所有可安装的软件包 可以通过 yum grouplist 来查看可能批量安装哪些列表 先装X windows #yum groupinstall 'X Win ...
- css display visibility
当visibility被设置为"hidden"的时候,元素虽然被隐藏了,但它仍然占据它原来所在的位置.注意,当元素被隐藏之后,就不能再接收到其它事件了. display属性就有一点 ...
- iOS KVC,KVO
链接(写得不错,着重kvc):http://www.cocoachina.com/industry/20140224/7866.html 链接:http://www.cnblogs.com/kensh ...
- light oj 1068 - Investigation 数位DP
思路:典型的数位DP!!! dp[i][j][k]:第i位,对mod取余为j,数字和对mod取余为k. 注意:由于32位数字和小于95,所以当k>=95时,结果肯定为0. 这样数组就可以开小点, ...
- 妙味课堂——HTML+CSS(第四课)(一)
这一课学的东西真是太多了,还不赶快记下来,留待以后慢慢回味! 首先我们回顾一下inline-block的特性: 使块元素在一行显示 使内嵌支持宽高 换行被解析了(问题) 不设置宽度的时候,宽度由内容撑 ...