(leetcode)Reverse Linked List 脑子已经僵住
Reverse a singly linked list.
参考http://www.2cto.com/kf/201110/106607.html
方法1:
讲每个节点的指针指向前面就可以。
/**
* 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 *q = NULL;
// if(!(head && head->next)) return NULL;
// ListNode *p = head->next;
// head->next = NULL; // while(p)
// {
// q=p->next;
// p->next=head->next;
// head->next=p;
// p = q;
// }
// return head; if((head == NULL) || (head->next==NULL)) return head;
ListNode *p = head;
ListNode *q = p->next;
ListNode *r = NULL;
head->next = NULL;
while(q)
{
r = q->next;
q->next = p;
p = q;
q = r;
}
head = p;
return head; }
};
方法二:这个方法想了很久才发现题目中的意思head是第一个节点
/**
* 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) {
if((head==NULL) || (head->next==NULL)) return head;
ListNode *q = NULL;
ListNode *p = head->next;//p位置不变 while(p->next)
{
q=p->next;
p->next = q->next;
q->next = head->next;
head->next = q;
}
p->next=head; //相当于成环
head=p->next->next; //新head变为原head的next
p->next->next=NULL; //断掉环
return head; }
};
太菜了!!!!!!
2015-8-2又重新写了一版,和第一种思路一样。
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if((head==NULL) || (head->next==NULL)) return head;
ListNode *q;
ListNode *p;//p位置不变
p = head;
q = head->next;
head->next = NULL;
while(q)
{
p = q;
q = q->next;
p->next = head;
head = p;
}
head = p;
return head;
}
};
(leetcode)Reverse Linked List 脑子已经僵住的更多相关文章
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [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 -- Reverse Linked List II -- 代码随笔(备忘)
题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...
- [leetcode]Reverse Linked List II @ Python
原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/ 题意: Reverse a linked list from positio ...
- [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] Reverse Linked List
Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...
- [Leetcode] Reverse linked list ii 反转链表
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2 ...
- leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...
- 翻转单链表 leetcode Reverse Linked List
翻转一个单链表.这个题目听说很多次了,总感觉肯定不是什么难题. 现在真的有点好高骛远了!总感觉那种很难的算法题才是难题,这种题没必要做.其实眼高手低啊. 这种easy题,我都不能一遍ac,这遇到白板编 ...
随机推荐
- NOI2012 : 迷失游乐园
终于补完NOI2012了好开心~ 题目大意:给定一棵树或者环套外向树,求出从中随机选一条简单路径的期望长度,环上点数不超过20. 设 d[x]表示x的度数,ch[x]表示x孩子个数 up[x]表示x向 ...
- BZOJ3024 : [Balkan2012]balls
问题1: ans=max(sum[n]-(sum[i]-sum[j-1])+a[i]*(i-j+1)) =max(sum[n]-sum[i]+sum[j-1]+a[i]*(i+1)-a[i]*j) = ...
- BZOJ3847 : ZCC loves march
注意到集结操作相当于合并一些点 于是我们可以使用并查集 对于每一行.每一列维护一个链表,储存里面的点 查询x时,在并查集中找到x的祖先u,此时x的坐标就是u的坐标 然后扫描u所在行列的链表,依次删除每 ...
- CentOS6.4 配置HAProxy+Keepalived
安装HAProxy请参考 http://www.cnblogs.com/kgdxpr/p/3272861.html 安装Keepalived 1.下载安装依赖包 yum install -y wget ...
- Codeforces Round #264 (Div. 2)
http://codeforces.com/contest/463 这场是我人生第一场cf啊.. 悲剧处处是啊. 首先,看不懂题,完全理解不了啊.都是wa了好几次才过的 所以a和b这两sb题我做了1个 ...
- C#的async和await
C# 5.0中引入了async 和 await.这两个关键字可以让你更方便的写出异步代码. 看个例子: public class MyClass { public MyClass() { Displa ...
- .net操作xml文件(新增.修改,删除,读取)---datagridview与xml文件
参考网址: http://www.cnblogs.com/liguanghui/archive/2011/11/10/2244199.html 很详细的,相信能给你一定的帮助.
- FLTK 1.3.3 MinGW 4.9.1 Configuration 配置
Download FLTK 1.3.3 Download CMake 3.2.0 Start CMake 3.2.0, fill the source and destination: source: ...
- ZooKeeper应用场景介绍
ZooKeeper是一个高可用的分布式数据管理与系统协调框架.维护着一个树形层次结构,书中的节点被称为znode.znode可以用来存储数据,并且有一个与之相关联的ACL(权限),znode不能大于1 ...
- 小组项目beta发布的评价
这次最看好飞天小女警组,相比上次他们的界面漂亮了很多,功能也相对完善,他们的礼物挑选系统非常有创意.如果去网上爬更多的数据,这个项目会更完美. 新蜂团队的俄罗斯方块游戏新增加了显示下一个方块以及游戏积 ...