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++的更多相关文章

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

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

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

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

  3. [leetcode]206. Reverse Linked List反转链表

    Reverse a singly linked list. Input: 1->2->3->4->5->NULL Output: 5->4->3->2- ...

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

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

  5. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

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

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

  7. [LintCode] Reverse Linked List 倒置链表

    Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...

  8. C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解

    面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...

  9. LeetCode 206. Reverse Linked List (倒转链表)

    Reverse a singly linked list. 题目标签:Linked List 题目给了我们一个链表,要求我们倒转链表. 利用递归,新设一个newHead = null,每一轮 把下一个 ...

随机推荐

  1. K-means算法应用:图片压缩

    plt.imshow(china[:,:,2]) plt.show() from sklearn.datasets import load_sample_image china=load_sample ...

  2. vue day6 分页显示

    @{ ViewBag.Title = "Home Page"; Layout = null; } <!DOCTYPE html> <html> <he ...

  3. web爬虫,BeautifulSoup

    BeautifulSoup 该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查找指定元素,从而使得在HTML或XML中查找指定元素变得简单. 1 2 3 ...

  4. 自己写的C#三层代码生成器

    思来想去用T4生成代码要学习它的语法,C#本身能很简单地生成txt文件,为啥不直接批量替换模板方式自己写个的三层代码生成器.说干就干,2个小时搞定.当然各层还可以做的更精细,比如DAL层的Add方法I ...

  5. Xshell5一打开就提示要使用该程序,请更新至最新版本

    网上有两种方法 方法一:临时 修改系统时间,修改为一年前的时间即可.但是你会发现修改回当前时间后,xshell又打不开了 方法二:替换xshell文件 找到xshell安装位置,如果是快捷方式,可以右 ...

  6. mac 版本navicate 如何安装破解版

    https://www.jianshu.com/p/f42785e55b6b  博客地址 部分童鞋安装后没有rpk文件,我也不知道怎么解决实在不行,请下载破解版链接:https://pan.baidu ...

  7. 【备份】如何在 PADS Layout 中选择 Gerber 274X 格式

    如何在 PADS Layout 中选择 Gerber 274X 格式. 起初原因是 JLC 说 274X 和 274D 的差别. 有小伙伴使用了 274D 的格式,结果做出来的 PCB 有问题.

  8. chromedriver与chrome各版本的对应关系表

    driver的下载地址 http://chromedriver.storage.googleapis.com/index.html 对应关系也可以查看 google官方的说明,通过当前浏览器版本找到对 ...

  9. 学习Python3 天眼查 爬虫

    刚开始学习Python,不愿意看基础,记忆不好,那些语法记不住,直接上个项目,这样比较深刻 刚好公司有个情况要查企业的信息,就想做个爬虫吧,有验证码的不愿意搞,那是个老大难问题,就选择了天眼查 过程都 ...

  10. 利用R与SAS进行关联规则挖掘

    一.利用R进行关联规则挖掘 数据结构如下: (共9个itemsets,5个items) 首先读入数据: demodata = read.transactions("C:\\Documents ...