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,每一轮 把下一个 ...
随机推荐
- 关于nginx安装、iptables设置和查看端口指令netstat/ss
实验1: Nginx介绍 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理 服务器,也是一个 IMAP/POP ...
- 引擎设计跟踪 地形LOD的改进
虽然地形已经有LOD和形变(geomorphing, 这里简称morphing)来进行LOD的渐变,从而避免bump, 但是如果LOD级别过多,远处的高山就会严重丢失细节,比如变成尖尖的凸起,非常丑陋 ...
- sql注入-推断是否存在SQL注入-单引号
来自:https://www.cnblogs.com/ichunqiu/p/5749347.html 首先我们需要了解数据是通过什么方式进行输入,这里我总结了三个: GET请求:该请求在URL中发送参 ...
- tolua 转换 std::shared_ptr
tolua 转换 std::shared_ptr 自从c++11以后std::shared_ptr几乎是比用的东西,经常会遇到类似如下应用 std::shared_ptr<Tst_ShareTe ...
- tcpdump+wireshark抓包分析
上一篇文章中,我们介绍了tcpdump如何抓包. tcpdump是命令行下便捷的抓包和分析工具,但使用方式不够友好, wireshark是带图形化界面的抓包和分析工具,操作简便,但需要主机有显示器. ...
- Hanlp自然语言处理工具之词法分析器
本章是接前两篇<分词工具Hanlp基于感知机的中文分词框架>和<基于结构化感知机的词性标注与命名实体识别框架>的.本系统将同时进行中文分词.词性标注与命名实体识别3个任务的子系 ...
- git 远程分支回滚
git代码库回滚: 指的是将代码库某分支退回到以前的某个commit id [本地代码库回滚]: git reset --hard commit-id :回滚到commit-id,讲commit-id ...
- idea jdk版本问题
问题描述: Information:Using javac 1.6.0_43 to compile java sourcesInformation:java: Errors occurred whil ...
- 代码漏洞扫描描述Cross Site History Manipulation解决办法[dongcoder.com]
代码漏洞扫描 漏洞描述:Cross Site History Manipulation 简要描述:产品的行为差异或发送不同的反应,在某种程度上暴露了与安全性相关的产品状态,例如特定的操作是否成功.可能 ...
- Oracle 外键级联更新
Oracle数据库中,外键约束只允许级联删除,不允许级联更新,因此,如果想要实现主表数据更新后,子表外键自动更新,只能取消外键关系,通过前端程序来维护实现完整引用,一个代替的解决方案是使用延迟约束和触 ...