lintcode 翻转链表
来源:互联网 发布:朋友圈 知乎 编辑:程序博客网 时间:2024/05/17 21:50
问题描述
http://www.lintcode.com/zh-cn/problem/reverse-linked-list/
样例
给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null
笔记
就是倒腾一下几个节点,找递推规律
代码
/** * Definition of ListNode * * class ListNode { * public: * int val; * ListNode *next; * * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */class Solution {public: /** * @param head: The first node of linked list. * @return: The new head of reversed linked list. */ ListNode *reverse(ListNode *head) { // write your code here if (head == NULL) return NULL; ListNode *pre = NULL; ListNode *tmp = head->next; while (head) { tmp = head->next; head->next = pre; pre = head; head = tmp; } return pre; }};
二次练习
/** * Definition of ListNode * * class ListNode { * public: * int val; * ListNode *next; * * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } */class Solution {public: /** * @param head: The first node of linked list. * @return: The new head of reversed linked list. */ ListNode *reverse(ListNode *head) { // write your code here ListNode* pre = NULL; ListNode* now = head; ListNode* nex = NULL; while (now != NULL) { nex = now->next; now->next = pre; pre = now; now = nex; } return pre; }};
0 0
- LintCode-翻转链表
- lintcode 翻转链表
- LintCode:翻转链表
- lintcode,翻转链表
- 翻转链表,lintcode
- Lintcode 翻转链表
- LintCode 翻转链表
- 翻转链表-LintCode
- LintCode : 翻转链表
- [Lintcode]翻转链表
- lintcode--翻转链表
- Lintcode翻转链表
- LintCode 翻转链表
- lintcode翻转链表
- LintCode 翻转链表
- Lintcode:翻转链表
- LintCode:翻转链表 II
- 【LintCode】翻转链表 II
- vagrant构建虚拟开发环境
- mybatis_SQL映射(2)出现的错误
- 关于C++中操作符重载的疑问 :四个运算符=, ->, [], ()不可以重载为全局函数(友员函数)
- studio 编译报错:compileSdkVersion 'android-24' requires JDK 1.8 or later to compile.
- ACM学习历程21——各种排列组合问题
- lintcode 翻转链表
- linux小记:Ubuntu和Arch下的Apache
- Cache之组相连映射
- Java ServletContext 详解
- 129. Sum Root to Leaf Numbers
- 也谈Python多线程(实例讲解)
- 基于ArcGIS API For JavaScript调用GP服务实现动态插值分析实现
- 主元分析(PCA)原理
- 健身