题目:

Reverse a linked list.

Example

For linked list 1->2->3, the reversed linked list is 3->2->1

题解:

Solution 1 ()

class Solution {
public:
ListNode *reverse(ListNode *head) {
if (!head) {
return head;
}
ListNode *prev = nullptr;
while (head) {
ListNode *tmp = head->next;
head->next = prev;
prev = head;
head = tmp;
} return prev;
}
};

  The basic idea of this recursive solution is to reverse all the following nodes after head. Then we need to set head to be the final node in the reversed list. We simply set its next node in the original list (head -> next) to point to it and sets its next to be NULL.

Solution 2 ()

class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (!head || !(head -> next)) return head;
ListNode* node = reverseList(head -> next);
head -> next -> next = head;
head -> next = NULL;
return node;
}
};

疑问?为啥下面这段程序是错的

class Solution {
public:
ListNode *reverse(ListNode *head) {
if (!head) {
return head;
}
ListNode *prev = nullptr;
while (head) {
ListNode *tmp = head;
head->next = prev;
prev = head;
head = tmp->next; } return prev;
}
};

  若一定要tmp保存head,那么程序应该如下

class Solution {
public:
ListNode *reverse(ListNode *head) {
if (!head) {
return head;
}
ListNode *prev = nullptr;
while (head) {
ListNode *tmp = new ListNode(-);
*tmp = *head;
head->next = prev;
prev = head;
head = tmp->next;
delete tmp;
} return prev;
}
};

解惑:

int main()
{
ListNode* tmp = new ListNode();
ListNode* p = new ListNode();
ListNode* q = new ListNode();
ListNode* r = new ListNode();
ListNode** t = &tmp;
tmp->next = p;
p->next = q;
q->next = r; t = &((*t)->next);
(*t) = (*t)->next; cout << (*t)->val << endl;
cout << tmp->next->val << endl;
cout << (*p).val << endl; return ;
}

输出为:2 2 1

【Lintcode】 035.Reverse Linked List的更多相关文章

  1. 【Lintcode】036.Reverse Linked List II

    题目: Reverse a linked list from position m to n. Given m, n satisfy the following condition: 1 ≤ m ≤ ...

  2. 【原创】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 ...

  3. 【LeetCode】206. Reverse Linked List (2 solutions)

    Reverse Linked List Reverse a singly linked list. click to show more hints. Hint: A linked list can ...

  4. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  5. 【LeetCode】206. Reverse Linked List

    题目: Reverse a singly linked list. 提示: 此题不难,可以用迭代或者递归两种方法求解.记得要把原来的链表头的next置为NULL: 代码: 迭代: /** * Defi ...

  6. 【leetcode】92. 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-> ...

  7. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  8. 【easy】206. Reverse Linked List 链表反转

    链表反转,一发成功~ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; ...

  9. 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List

    The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three poi ...

随机推荐

  1. linux脚本实现自己主动输入password

    使用Linux的程序猿对输入password这个举动一定不陌生,在Linux下对用户有严格的权限限制,干非常多事情越过了权限就得输入password.比方使用超级用户运行命令,又比方ftp.ssh连接 ...

  2. firfox浏览器常用快捷键

    Ctrl + 数字键来打开第N个标签页这种还要先数完再到键盘上找数字Ctrl + Page Up = 激活左边一个标签页Ctrl + Page Down = 激活右边一个标签页Ctrl + Tab = ...

  3. mongodb启动不了:提示错误信息为 child process failed, exited with error number 100

    [启动mongo 副本集错误提示]: [原因分析说明]: 查询很多资料得知由于上次使用了暴力关闭系统或者DB,导致数据文件锁住. [解决办法]: 1.  在 mongo.conf 文件添加一下属性值  ...

  4. 洛谷3243 [HNOI2015]菜肴制作

    题目戳这里 Solution 错误的想法:正向建图,然后从入度为0的点选出最小u的开始输出,然后找出u连接的点v,并把v的度数减一,再次把入度为0的点加入小根堆,这样显然有错,因为只能局部保证最小,后 ...

  5. case_for_if 各种嵌套相结合

    注明:本文是参考其他相关文章 整理,完全尊重原著作 #!/bin/bash usage() { cat << EOF EOF } main() { echo "猜分数赢大奖(0- ...

  6. 在tomcat下直接访问Html报错,说找不到资源(404)

    今天由于工作需要,想把一个html直接放到tomcat(干净的tomcat,没有做过任何修改.)下进行访问,然后根据经验就直接在webapps下创建了个文件夹test,然后把需要的test.html拷 ...

  7. linux日志系统介绍 —— syslog(),openlog(),closelog()

    函数使用介绍 这里面的三个函数openlog, syslog.closelog是一套系统日志写入接口.另外那个vsyslog和syslog功能一样,仅仅是參数格式不同.         通常.sysl ...

  8. 9patch图片

    9patch图片可直接缩放,放在drawable文件夹下就可以 右边和下边指定内容区域

  9. office 2010 激活工具 迷你KMS使用说明

    “迷你KMS”——“”体态轻盈仅只1MB.但却一身承担两大职能:KMS服务器和客户激活端.“迷你KMS”可激活Microsoft Office 2010系列三大版本,以及Windows 7企业.专业版 ...

  10. ubuntu mysql 配置(远程访问&&字符集设置&&忽略大小写)

    1.安装 参考http://www.cnblogs.com/wuhou/archive/2008/09/28/1301071.html sudo apt-get install mysql-serve ...