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 脑子已经僵住的更多相关文章

  1. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  2. [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-> ...

  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-p ...

  4. [leetcode]Reverse Linked List II @ Python

    原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/ 题意: Reverse a linked list from positio ...

  5. [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-> ...

  6. [LeetCode] Reverse Linked List

    Reverse a singly linked list. 这题因为palindrome linked list 的时候需要就顺便做了一下.利用三个指针:prev, now, next 相互倒腾就行. ...

  7. [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 ...

  8. 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-> ...

  9. 翻转单链表 leetcode Reverse Linked List

    翻转一个单链表.这个题目听说很多次了,总感觉肯定不是什么难题. 现在真的有点好高骛远了!总感觉那种很难的算法题才是难题,这种题没必要做.其实眼高手低啊. 这种easy题,我都不能一遍ac,这遇到白板编 ...

随机推荐

  1. kmp 和boyer-moore

    <html> <head> <meta http-equiv="content-type" content="text/html; char ...

  2. 使用CSS修改HTML5 input placeholder颜色

    HTML <input type="text" placeholder="Value" /> 有三种实现方式:伪元素(pseudo-elements ...

  3. 【BZOJ】1074: [SCOI2007]折纸origami

    http://www.lydsy.com/JudgeOnline/problem.php?id=1074 题意:一开始有一个左上角是(0,100),右下角是(100,0)的纸片,现在可以沿有向直线折n ...

  4. 【BZOJ】1015: [JSOI2008]星球大战starwar(并查集)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1015 看了题解的囧T_T,一开始以为是求割点,但是想到割点不能统计.... 这题用并查集,思想很巧妙 ...

  5. Dynamic LINQ OrderBy

    public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string pr ...

  6. asp.net 微信企业号办公系统-流程设计--流程步骤设置-事件设置

    事件设置是设置当前步骤在提交前后或退回前后要执行的一些操作(该事件为服务器事件). 事件格式为:dll名称.命名空间名称.类名.方法名,这里不需要写括号和参数,处理时会自动带上当前流程实例的相关参数. ...

  7. [文字雲產生器] Tagxedo 把文字串成雲、變成畫,印在 T-Shirt、馬克杯、詩袋….

    http://www.tagxedo.com/app.html 有種東西叫「Word Clouds」,就是把一堆文字依照不同的大小.顏色.角度與位置拼湊在一起,讓他變成像一朵雲一般.組合成各種不同的形 ...

  8. Apache Spark源码走读之16 -- spark repl实现详解

    欢迎转载,转载请注明出处,徽沪一郎. 概要 之所以对spark shell的内部实现产生兴趣全部缘于好奇代码的编译加载过程,scala是需要编译才能执行的语言,但提供的scala repl可以实现代码 ...

  9. Linux 计划任务 Crontab 笔记与总结(1)

    Linux 版本:CentOS 6.6 应用场景,例如: ① 每分钟执行一个程序检查系统运行状态 ② 每天凌晨需要对过去一天的业务数据进行统计 ③ 每个星期需要把日志文件备份 ④ 每个月把数据库进行备 ...

  10. Windows远程桌面连接Mac OS X

    Windows远程桌面连接Mac OS X   第一步:Mac OS X 10.5 已经增加支持了由VNC Viewer访问的功能,设置如下:   系统偏好设置-共享-勾选“屏幕共享”,然后在电脑设置 ...