/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution
{
Stack<ListNode> S = new Stack<ListNode>();
private void SaveList(ListNode node)
{
if (node != null)
{
S.Push(node); if (node.next != null)
{
SaveList(node.next);
}
}
} private ListNode GetLastNode(ListNode head)
{
while (head.next != null)
{
head = head.next;
}
return head;
} public ListNode ReverseList(ListNode head)
{
SaveList(head);
ListNode newhead = null;
while (S.Count > )
{
var curnode = S.Pop();
curnode.next = null;
if (newhead == null)
{
newhead = curnode;//找到链头
}
else
{
var prenode = GetLastNode(newhead);//找到新的链尾
prenode.next = curnode;
}
}
return newhead;
}
}

https://leetcode.com/problems/reverse-linked-list/#/description

简化的代码:

 public class Solution
{
public ListNode ReverseList(ListNode head)
{
//ListNode p = head;
ListNode n = null;
while (head != null)
{
ListNode temp = head.next;
head.next = n;
n = head;
head = temp;
}
return n;
}
}

补充一个python的实现:

 class Solution(object):
def reverseList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
temp = ListNode()
while head != None:
nextnode = head.next
head.next = temp.next
temp.next = head
head = nextnode
return temp.next

leetcode206的更多相关文章

  1. 【LeetCode206】Reverse Linked List★

    题目描述: 解题思路: 关于单链表的反转有迭代和递归两种方法,方法不在多,本文主要介绍迭代的方法. 迭代的方法,要使用三个指针,需要注意一点的是指针的初始化,对第一个指针初始化为pre=null,第二 ...

  2. leetcode206 反转链表 两种做法(循环,递归)

    反转链表 leetcode206 方法1 循环 public ListNode reverseList(ListNode head) { if (head == null || head.next = ...

  3. 【数据结构】单链表介绍及leetcode206题反转单链表python实现

    题目传送门:https://leetcode-cn.com/problems/reverse-linked-list/ 文章目录 单链表介绍 链表 概念 种类 优缺点 单链表(slist) leetc ...

  4. 链表反转leetcode206

    最近准备结束自己的科研生涯,准备要开始找工作了,准备在LEETCODE刷刷题...刷的前40题全部用python刷的,各种调包速度奇快,后被师哥告知这样没意义,于是准备开始回归C++,Python用的 ...

  5. Leetcode-206 Reverse Linked List

    #206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...

  6. 每天一道LeetCode--206. Reverse Linked List

    Reverse a singly linked list. package cn.magicdu; import cn.magicdu.extra.ListNode; public class _20 ...

  7. [Swift]LeetCode206. 反转链表 | Reverse Linked List

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

  8. LeetCode-206.ReverseLinked List

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

  9. leetcode206. 反转链表

    1:迭代法 假设存在链表 1 → 2 → 3 → Ø,我们想要把它改成 Ø ← 1 ← 2 ← 3. 在遍历列表时,将当前节点的 next 指针改为指向前一个元素.由于节点没有引用其上一个节点,因此必 ...

随机推荐

  1. MVC5 Api Area 区域

    到Area区分不同的模块让项目结构更加的清晰 TODO 步骤如下: 项目 –> 添加 -> 区域 (Area) 添加路由规则 public static class WebApiConfi ...

  2. pthon入门之strip()和split()函数简单区分

    小白,分享记录学习新感悟 路飞的第一次作业写一个登录的程序,作业的升级需求中有个锁文件的需求,大致上如果用户数错了密码三次将用户写到黑名单上,下次登录锁定: ok基本的要求写完,我们上代码 usern ...

  3. Javascript中的this指向。

    一.JavaScript中的函数 在了解this指向之前,要先弄明白函数执行时它的执行环境是如何创建的,这样可以更清楚的去理解JavaScript中的this指向. function fn(x,y,n ...

  4. window.location.href webkit不兼容

    window.event.returnValue=false; 在location.href后加上后修复 来自为知笔记(Wiz)

  5. Python全栈之路----函数进阶----闭包

    关于闭包,即函数定义和函数表达式位于另一个函数的函数体内(嵌套函数).而且,这些内部函数可以访问它们所在的外部函数中声明的所有局部变量.参数.当其中一个这样的内部函数在包含它们之外被调用时,就会形成闭 ...

  6. 测试那些事儿—postman入门介绍

    1.postman入门介绍 一款功能强大的网页调试与发送网页HTTP请求的工具. 1)模拟各种HTTP请求,如get,post,put,delete 2)测试集合Collection    Colle ...

  7. webpack初步学习

    https://segmentfault.com/a/1190000006178770 该篇文章足够webpack入门的学习了,对webpack有个初步的了解和认识.

  8. 软件工程第四次作业 石墨文档IOS

    待分析的产品:石墨文档IOS客户端 作业地址: https://edu.cnblogs.com/campus/nenu/2016CS/homework/2505   第一部分 调研, 评测 1.下载并 ...

  9. 我的代码-models

    # coding: utf-8 # In[1]: import pandas as pdimport numpy as npfrom sklearn import treefrom sklearn.s ...

  10. hsdfz -- 6.16 -- day1

    恩这回不写游记了 按照老师要求记录今天的心里路程:这题似乎可做期望得分150->日部分分似乎不是很显然->a题似乎是结论题,大力猜一波结论->过不了样例,先看b题->b题动态树 ...