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. CentOS6.4 配置Tengine

    1.安装Nginx所需的pcre-devel库 yum install -y gcc gcc-c++ wget ftp://ftp.csx.cam.ac.uk/pub/software/program ...

  2. tableviewcell的这贴状态和传值总结

    01  控制器 1.1 定义一个可变数组存放数据,再定义一个可变数组来记录分组的折叠状态 @property(nonatomic)NSMutableArray *dataArr; //记录所有分组的折 ...

  3. hello world 驱动程序编写

    操作系统课程设计选题  驱动程序的编写和安装. 经过一天多的努力,终于把我的第一个驱动程序模块成功编写并实现插入内核和移除,在这里把过程记录下来方便以后查看,也给其他为之困扰的朋友一个建议. 环境: ...

  4. 为什么java里用常量赋值就相等,用字符串就不等?

    例一: String s0="HF"; String s1=new String("HF"); System.out.println(s0==s1); 输入为什 ...

  5. 【iHMI43 4.3寸液晶模块】demo竖屏例程(版本1.01)发布

    ============================== 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:h ...

  6. POJ 2777 Count Color(线段树染色,二进制优化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42940   Accepted: 13011 Des ...

  7. ionic 写一个五星评价(非指令)

    Controller里的代码: .controller('evaluateCtrl', function($scope, $state, $stateParams, $ionicPopup,$ioni ...

  8. 动态input file多文件上传到后台没反应的解决方法!!!

    其实我也不太清除具体是什么原因,但是后面就可以了!!! 我用的是springMVC 自带的文件上传 1.首先肯定是要有springMVC上传文件的相关配置! 2.前端 这是动态input file上传 ...

  9. 用ultraISO 制作一个MSdos启动软盘镜像

    见过软盘,但是没用过,在虚拟机里试试. 磁带,软盘,光盘,硬盘…… 储存介质一代代更新,看到的img.iso文件都是叫做镜像文件(image file ).image 即图片照片,所谓的image f ...

  10. git命令常见问题总结

    1.git如何放弃所有本地修改 git checkout . #本地所有修改的.没有的提交的,都返回到原来的状态 git stash #把所有没有提交的修改暂存到stash里面.可用git stash ...