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,每一轮 把下一个 ...
随机推荐
- STM8L LCD配置与com使用问题
void LCD_GPIO_Config(void) { //SEG GPIO Init GPIO_Init(GPIOE, GPIO_Pin_0|GPIO_Pin_1,GPIO_Mode_Out_PP ...
- 基于mpvue搭建微信小程序
mpvue是美团开源的一套语法,语法与vue.js一致,快速开发小程序的前端框架.框架基于vue.js核心,修改了vue.js的runtime和compiler实现,使用此框架,开发者可以完全使用vu ...
- angular1中ng-repeat效率优化方法:
1.当 ng-repeat 的数组被替换时, 它默认并不会重新利用已有的 Dom 元素,而是直接将其全部删除并重新生成新的数组 Dom 元素: 2.Dom 的频繁操作是非常不友好的, ng-repea ...
- Git与Github的连接与使用
下面继续,使用git 将项目上传到GitHub上 首先要有GitHub账号,这就不用说了,没有的先注册,地址:https://github.com 没有仓库的话,先新创建一个仓库 填写新仓库名称,备注 ...
- TCL 引用同目录下其他脚本文件--source
方法一: source [file join [file dirname [info script]] Export_inp_by_loadstep.tcl] 方法二: source [file jo ...
- 【C++】如何接收函数return返回来的数组元素
转自 https://www.cnblogs.com/Wade-James/p/7965775.html 我们知道return语句作为被调用函数的结束,返回给调用者函数值.一般来说,是返回一个函数值, ...
- webstorm的相关操作
1.webstorm修改tab键的缩进
- Charles更新至4.2.8特别版
下载地址:链接:https://pan.baidu.com/s/1c2wXdBE 提取码:g7qc 更多工具/教程可进入Q群:测试技术学习小组 478717918
- kafka性能调优(转)
原文 https://blog.csdn.net/weixin_39478115/article/details/79155287 Broker参数配置 1.网络和io操作线程配置优化 # brok ...
- python 开发接口(一)
一.首先导入模块 pip install flask 二 1 import flas 2 import json import flask #1.启动一个服务 #2.接收到客户端传过来的数据 #3. ...