LeetCode 206. Reverse Linked List倒置链表 C++
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
解法一:(C++)
利用迭代的方法依次将链表元素放在新链表的最前端实现链表的倒置
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* newhead=NULL;
while(head){
ListNode* t=head->next;
head->next=newhead;
newhead=head;
head=t;
}
return newhead;
}
};
解法二(C++)
并不是多么正统的方法,借助vector的先进后出的方法实现倒置
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head||!head->next)
return head;
vector<int> m;
while(head){
m.push_back(head->val);
head=head->next;
}
ListNode* newhead=new ListNode(-);
ListNode* t=newhead;
while(!m.empty()){
ListNode* cur=new ListNode(m.back());
m.pop_back();
t->next=cur;
t=cur;
}
return newhead->next;
}
};
LeetCode 206. Reverse Linked List倒置链表 C++的更多相关文章
- [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-> ...
- [leetcode]206. Reverse Linked List反转链表
Reverse a singly linked list. Input: 1->2->3->4->5->NULL Output: 5->4->3->2- ...
- leetcode 206. Reverse Linked List(剑指offer16)、
206. Reverse Linked List 之前在牛客上的写法: 错误代码: class Solution { public: ListNode* ReverseList(ListNode* p ...
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- [LintCode] Reverse Linked List 倒置链表
Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- LeetCode 206. Reverse Linked List (倒转链表)
Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...
随机推荐
- 使用[].forEach.call()方法的写拖拽排序。
---恢复内容开始--- }); });}要引用两个插件: <script src="./jquery.1.12.4.min.js"></script> & ...
- Windows与Linux的命令行命令对比
Windows与Linux的命令行命令对比 * Windows不区分大小写,Linux区分大小写的. sn DOS Command UNIX Equivalent Effect 影响 1 ASSIGN ...
- Beff的学习
Beff介绍 BeEF,全称The Browser Exploitation Framework,是一款针对浏览器的渗透测试工具. 首先打开kali,直接点击beef图标打开beef 浏览器会默认弹出 ...
- BootStrap的table技术小结:数据填充、分页、列宽可拖动
本文结构:先说明,后代码.拷贝可直接运行. 一.demo结构: 二.文件引入 这些里面除了下面2个比较难找,其他的都很好找 bootstrap-table-resizable.js colResiza ...
- todos+增删改查+js练习
增删改查+js练习+es6字符串模板@haloBabyBear <!DOCTYPE html> <html lang="en"> <head> ...
- 转载一篇阿里云Terraform 开发指南
连接:https://www.jianshu.com/p/0aebea117cae 是一个比较详细的开发指南
- 解决thinkPHP3.2.3使用Smarty模板后无法使用系统常量问题
https://blog.csdn.net/u014520745/article/details/52029411 在ThinkPHP/Library/Think/View.class.php 输出模 ...
- java-项目中无法访问js、css等静态资源
解决方法:在mvc.xml配置文件中增加如下配置 如果增加<mvc:default-servlet-handler/> 后无法访问controller,需要增加<mvc:annota ...
- Dubbo-Admin 2.6.0使用
一.下载源码 下载2.6.0的源码 https://github.com/apache/incubator-dubbo/releases/tag/dubbo-2.6.0 二.使用Eclipse打开du ...
- Windows硬链接 软链接 符号链接 快捷方式
http://blog.nsfocus.net/shortcuthard-linkjunction-pointsymbolic-link/ Windows支持4种”链接”机制,分别是shortcut. ...