LC 206. Reverse Linked List
题目描述
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的更多相关文章
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- 链表 206 Reverse Linked List, 92,86, 328, 2, 445
表不支持随机查找,通常是使用next指针进行操作. 206. 反转链表 /** * Definition for singly-linked list. * struct ListNode { * i ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 206. Reverse Linked List - LeetCode
Question 206. Reverse Linked List Solution 题目大意:对一个链表进行反转 思路: Java实现: public ListNode reverseList(Li ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到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 ...
- [LeetCode] 206. Reverse Linked List 反向链表
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...
- [LeetCode] 206. Reverse Linked List ☆(反转链表)
Reverse Linked List 描述 反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3-> ...
- [刷题] 206 Reverse Linked List
要求 反转一个链表 不得改变节点的值 示例 head->1->2->3->4->5->NULL NULL<-1<-2<-3<-4<-5 ...
随机推荐
- ELK系列(7) - 测试环境下Logstash异常退出:block in multi_receive_encoded
问题与分析 在本地测试无误后将ELK部署到了测试环境,结果第二天发现Logstash挂掉了,一开始以为是自动部署之类导致的问题.之后几天时间里Logstash总是会down掉,查看了下日志文件,发现报 ...
- 提高十连测day3
提高十连测day3 A 我们可以枚举两个 $ 1 $ 之间的相隔距离,然后计算形如 $ 00100100 \cdots $ 的串在原串中最⻓⼦序列匹配即可,复杂度 $ O(n^2) $ .寻找 $ S ...
- 深入理解JVM虚拟机3:垃圾回收器详解
JVM GC基本原理与GC算法 Java的内存分配与回收全部由JVM垃圾回收进程自动完成.与C语言不同,Java开发者不需要自己编写代码实现垃圾回收.这是Java深受大家欢迎的众多特性之一,能够帮助程 ...
- [转]解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists).
Git fetch和git pull的区别: 都可以从远程获取最新版本到本地 1.Git fetch:只是从远程获取最新版本到本地,不会merge(合并) $:git fetch origin mas ...
- php手记之04-tp5数据库操作
//--------查询// 原生sql语句查询 // $ret = Db::query("select * from tp5_user where id>10"); // ...
- Qt之模型/视图(委托)
概念 不同于模型 - 视图 - 控制器模式,模型/视图设计不包括用于管理与用户交互的一个完全独立的组件.一般情况,视图负责将模型数据呈现给用户以及处理用户输入.为了输入更加具有灵活性,则由委托来执行交 ...
- C++在线编程网站
1.推荐 http://www.dooccn.com/cpp/ 2.https://wandbox.org/ 3.https://www.tutorialspoint.com/compile_cpp_ ...
- vue页面刷新重定向
在App.vue中,添加如下代码: created(){ if(this.$router.path !== '/RealTimeMonitoring'){ this.$router.replace(' ...
- 算法习题---4-4骰子涂色(UVa253)
一:题目 分别对两个骰子的六个面涂色r-红 b-蓝 g-绿,通过转动骰子,看两个骰子是不是一样的涂色方法 (一)题目详解 题目规定了正方体的六个面的序号:从1-,按照这个需要提供涂色序列 (二)案例展 ...
- 一秒 解决 ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql 问题
提示:ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'.前两天也出现过这个问题,网上找了一个比 ...