题目描述

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

参考答案

 /**
* 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* cur = NULL;
while(head){
ListNode * temp = head->next;
head -> next = cur;
cur = head;
head = temp;
}
return cur;
}
};

补充说明

term 1:

temp = 2 3 4 5 null

head = 1 null = 1 2 3 4 5 null + null

cur = 1 null

head = 2 3 4 5 null

term 2:

temp = 3 4 5 null

head = 2 1 null ( 2 3 4 5 null + 1 null)

cur = 2 1 null

head = 3 4 5 null

term 3:

temp = 4 5 null

head = 3 2 1 null = 3 4 5 null + 2 1 null

cur = 3 2 1 null

head = 4 5 null

......

LC 206. Reverse Linked List的更多相关文章

  1. leetcode 206. Reverse Linked List(剑指offer16)、

    206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...

  2. 链表 206 Reverse Linked List, 92,86, 328, 2, 445

    表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...

  3. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  4. 206. Reverse Linked List - LeetCode

    Question 206. Reverse Linked List Solution 题目大意:对一个链表进行反转 思路: Java实现: public ListNode reverseList(Li ...

  5. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  6. 【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 ...

  7. [LeetCode] 206. Reverse Linked List 反向链表

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  8. [LeetCode] 206. Reverse Linked List ☆(反转链表)

    Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL    输出: 5->4->3-> ...

  9. [刷题] 206 Reverse Linked List

    要求 反转一个链表 不得改变节点的值 示例 head->1->2->3->4->5->NULL NULL<-1<-2<-3<-4<-5 ...

随机推荐

  1. ROS与树莓派的结合

    从零开始学树莓派和ROS 今天写下自己的第一篇博客,记录一下自己的学习历程和学习过程中碰到的各种小问题,供同道者参阅和自己以后回顾用 ,水平不高,我就放开手写吧,反正也不会有人看. 我现在在做毕业设计 ...

  2. 20191214数组习题之三:报数(附pow函数简单用法)

  3. [Codeforces1148C]Crazy Diamond——构造

    题目链接: [Codeforces1148C]Crazy Diamond 题目大意: 给出一个$1\sim n$的排列要求将其排序,每次能交换两个位置的数当且仅当这两个位置下标差的绝对值大于等于$\f ...

  4. POI2010 Bridges

    好题\(Q\omega Q\) 我们考虑这个东西要求最大值最小,显然一眼二分答案对吧. 问题在于如何\(check\),我们二分答案之后把问题转换成了混合图如何求欧拉回路. 考虑欧拉回路的性质,每一个 ...

  5. js对元素判断

    $("input[type='text']").attr("readonly","readonly"); $("textarea& ...

  6. fixedFluxPressure边界条件【转载】

    转载自:http://blog.sina.com.cn/s/blog_e256415d0102vikh.html fixedFluxPressure是OpenFOAM较新的一个边界条件,表示边界处压力 ...

  7. WORD转HTML-python第三方包Mammoth(官方文档翻译)

    Mammoth 官方 Mammoth可用于将.docx文档(比如由Microsoft Word创建的)转换为HTML.Mammoth致力于通过文档中的语义信息生成简洁的HTML,而忽略一些其他细节.例 ...

  8. 5.linux 软件安装的三种方法

      一.linux 操作系统中 软件的分类 以及软件的安装     vmtools  调用了perl语言写的安装脚本去进行内核的升级安装  ./ xxxxx        源码包安装软件:GNU  使 ...

  9. win10 sedlauncher.exe占用cpu处理

    打开应用和功能,搜KB4023057,然后卸载. 打开系统服务,找到Windows Remediation Service (sedsvc)和Windows Update Medic Service ...

  10. matlab中x.^2与x^2有什么区别?

    .^2是矩阵中的每个元素都求平方,^2是求矩阵的平方或两个相同的矩阵相乘,因此要求矩阵为方阵,且看下面的例子x=1:4x = 1 2 3 4 x.^2 ans = 1 4 9 16 x^2 Error ...