/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* recursive(ListNode* head) {
ListNode* pre_node;
if(head->next!=NULL)
{
pre_node=recursive(head->next);
ListNode* cur_pre_node=pre_node;//找到新的节点后,对末尾加入当前节点
while(cur_pre_node->next!=NULL)
cur_pre_node=cur_pre_node->next;
cur_pre_node->next=head;
head->next=NULL;//别忘了末尾补NULL
}
else
pre_node=head;//如果是尾节点,就定义为新的头结点 return pre_node; } ListNode* iteration(ListNode* head)
{
ListNode* node=new ListNode(head->val);
head=head->next; while(head!=NULL)
{
ListNode* cur_node=new ListNode(head->val);//每次开辟新空间
cur_node->next=node;//指向node
node=cur_node;//node后移
head=head->next;
}
return node;
} ListNode* reverseList(ListNode* head) {
if(head==NULL)
return NULL;
//return recursive(head);//方法一 递归
return iteration(head);//方法一 迭代
}
};

分析:

嗯,如程序所示。

leecode第二百零六题(反转链表)的更多相关文章

  1. leecode第二百三十七题(删除链表中的节点)

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  2. leecode第二百九十二题(Nim游戏)

    class Solution { public: bool canWinNim(int n) { )==)//用与的时候,要注意优先级问题 //用n%4==0的时候,其耗时比用&短,但是空间消 ...

  3. leecode第二百三十一题(2的幂)

    class Solution { public: bool isPowerOfTwo(int n) { bool is_flag=false; ) { ==)//如果为1,看是不是第一个1 { if( ...

  4. leecode第二百三十题(二叉搜索树中第K小的元素)

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  5. leecode第一百零四题(二叉树的最大深度)

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  6. 【leetcode 简单】 第一百零六题 压缩字符串

    给定一组字符,使用原地算法将其压缩. 压缩后的长度必须始终小于或等于原数组长度. 数组的每个元素应该是长度为1 的字符(不是 int 整数类型). 在完成原地修改输入数组后,返回数组的新长度. 进阶: ...

  7. 第二百零六节,jQuery EasyUI,Menu(菜单)组件

    jQuery EasyUI,Menu(菜单)组件 学习要点: 1.加载方式 2.菜单项属性 3.菜单属性 4.菜单事件 5.菜单方法 本节课重点了解 EasyUI 中 Menu(菜单)组件的使用方法, ...

  8. 第四百零六节,自定义用户表类来继承Django的用户表类,

    第四百零六节,自定义用户表类来继承Django的用户表类, models.py from django.db import models # Create your models here. from ...

  9. 第二百九十六节,python操作redis缓存-Hash哈希类型,可以理解为字典类型

    第二百九十六节,python操作redis缓存-Hash哈希类型,可以理解为字典类型 Hash操作,redis中Hash在内存中的存储格式如下图: hset(name, key, value)name ...

随机推荐

  1. HTML调用PC摄像头【申明:来源于网络】

    HTML调用PC摄像头[申明:来源于网络] ---- 地址:http://www.oschina.net/code/snippet_2440934_55195 <!DOCTYPE html> ...

  2. CentOS使用systemctl daemon-reload报错Error getting authority: Error initializing authority: Error calling StartServiceByName for org.freedesktop.PolicyKit1: Timeout was reached (g-io-error-quark, 24)解决办法

    CentOS修改了系统启动文件后需要重载报错 systemctl daemon-reload Error getting authority: Error initializing authority ...

  3. [No000018A]改善C#程序的建议11-20

    建议11:区别对待 == 和Equals CLR中将“相等性”分为两类:1.值相等性:两个变量包含的数值相等.2.引用相等性:两个变量引用的是内存中的同一个对象. 但并不是所有的类型的比较都是按照其本 ...

  4. React中redux表单编辑

    reduxForm中反写数据在输入框中,数据是从别的模块拉取 // 编辑应用表单 class EditCode extends React.Component { constructor(props) ...

  5. python 发送无附件邮件

    import smtplibimport tracebackfrom email.mime.text import MIMETextfrom config.config import *        ...

  6. 20165321 测试-3-ch02

  7. Could not open JDBC Connection for transaction; nested exception is com.alibaba.druid.pool.GetConnection

    Could not open JDBC Connection for transaction; nested exception is com.alibaba.druid.pool.GetConnec ...

  8. tp 内置压缩文件zip

    一.解压缩zip文件 $zip = new ZipArchive;//新建一个ZipArchive的对象 /* 通过ZipArchive的对象处理zip文件 $zip->open这个方法的参数表 ...

  9. C#串口通信遇到的坑

    C#串口通信中有一个DataReceived事件可以委托一个接收函数.此接收函数是运行在辅线程(secondary thread)上的.如果要在这个函数中修改主线程中的一些元素,比如UI界面上的变量的 ...

  10. DB2 错误代码

    sqlcode sqlstate 说明 000 00000 SQL语句成功完成 01xxx SQL语句成功完成,但是有警告 +012 01545 未限定的列名被解释为一个有相互关系的引用 +098 0 ...