题目:

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. An easy to use android color picker library

    https://github.com/xdtianyu/ColorPicker

  2. canvas drawImage方法不显示图片的解决方案

    先复习一下用法: context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height); 各个参数说明: 参数 描述 img 规定要使用的图像.画布 ...

  3. java 中的final

    在编程语言中都有某种方式,告知编译器一块数据是恒定不变的.有两个需求 1. 一个永不改变的编译器常量 2. 一个在运行时被初始化的值,而这个值不会被改变 在Java中,使用final修饰变量实现这两个 ...

  4. uva 11404 dp

    UVA 11404 - Palindromic Subsequence 求给定字符串的最长回文子序列,长度一样的输出字典序最小的. 对于 [l, r] 区间的最长回文串.他可能是[l+1, r] 和[ ...

  5. 【Python】selenium调用IE11浏览器,报错“找不到元素”NoSuchWindowException: Message:Unable to find element on closed window

    当编写自动化脚本,定位浏览器元素时,报如下错误: 代码: >>> # coding=utf-8 >>> from selenium import webdriver ...

  6. MongoDB水平分片集群(转)

    为何需要水平分片 1 减少单机请求数,将单机负载,提高总负载 2 减少单机的存储空间,提高总存空间. 下图一目了然: mongodb sharding 服务器架构 简单注解: 1 mongos 路由进 ...

  7. [Python]xlrd 读取excel 日期类型2种方式

    有个excle表格须要做一些过滤然后写入数据库中,可是日期类型的cell取出来是个数字,于是查询了下解决的办法. 主要的代码结构 data = xlrd.open_workbook(EXCEL_PAT ...

  8. runsv

    runsv(8) manual page http://smarden.org/runit/runsv.8.html Name runsv - starts and monitors a servic ...

  9. curl post 请求 es 数据 REST 批量删除

    curl  -d  "keyword=牛" http://api.com/path/index curl模拟http发送get或post接口测试 - 一生守候 - 博客园 http ...

  10. Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fromHere.com' is therefore not allowed access.

    Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is presen ...