/**
* 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. 拉格朗日乘子法&KKT条件

    朗日乘子法(Lagrange Multiplier)和KKT(Karush-Kuhn-Tucker)条件是求解约束优化问题的重要方法,在有等式约束时使用拉格朗日乘子法,在有不等约束时使用KKT条件.前 ...

  2. django rest_framework 分页出现报错

    1.出现: WARNINGS: ?: (rest_framework.W001) You have specified a default PAGE_SIZE pagination rest_fram ...

  3. ecmall 学习记录2

    1.ecmall 自带的写入日志方法:do_log4php("函数名","类名",$param);   $param是参数 在类里调用写入之日的方法 需要先加载 ...

  4. python用schedule模块实现定时任务

    1 import schedule 2 import time 3 4 def test(): 5 print("I'm working...") 6 def test2(): 7 ...

  5. 使用pip命令自动生成项目安装依赖清单

    Python项目中经常会带requirements.txt文件,里面是项目所依赖的包的列表,也就是依赖关系清单,这个清单也可以使用pip命令自动生成. pip命令: 1 pip freeze > ...

  6. jQuery-2.DOM---创建节点及节点属性

    DOM创建节点及节点属性 通过JavaScript可以很方便的获取DOM节点,从而进行一系列的DOM操作.但实际上一般开发者都习惯性的先定义好HTML结构,但这样就非常不灵活了. 试想下这样的情况:如 ...

  7. 自定义textview

    #import <UIKit/UIKit.h> @class FSTextView; typedef void(^FSTextViewHandler)(FSTextView *textVi ...

  8. 《从Lucene到Elasticsearch:全文检索实战》学习笔记二

    今天我给大家讲讲分词算法 分词算法概述:词是语义的最小单位.分词对搜索引擎的作用很大,可以促进搜索引擎程序自动识别语句的含义,可以提高搜索结果的匹配度,分析的质量也将直接影响了搜索结果的精确度.分词存 ...

  9. vim编辑Makefile如何使用Tab

    因为用vim编辑代码设置了Tab键为4个空格,但有时候我们需要编写Makefile,必须使用Tab,同时也不想设置set noexpandtab. 其实可以先Ctrl_v组合键,再按Tab键盘,这样我 ...

  10. py-day2-4 python 集合

    # 集合是由 { ,} 组成 test = {1,2,8,9,7,5} print(test) {1, 2, 5, 7, 8, 9} # 集合的结果是 去重的,且排序是 无序的 test = {1,2 ...