leetcode206
/**
* 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的更多相关文章
- 【LeetCode206】Reverse Linked List★
题目描述: 解题思路: 关于单链表的反转有迭代和递归两种方法,方法不在多,本文主要介绍迭代的方法. 迭代的方法,要使用三个指针,需要注意一点的是指针的初始化,对第一个指针初始化为pre=null,第二 ...
- leetcode206 反转链表 两种做法(循环,递归)
反转链表 leetcode206 方法1 循环 public ListNode reverseList(ListNode head) { if (head == null || head.next = ...
- 【数据结构】单链表介绍及leetcode206题反转单链表python实现
题目传送门:https://leetcode-cn.com/problems/reverse-linked-list/ 文章目录 单链表介绍 链表 概念 种类 优缺点 单链表(slist) leetc ...
- 链表反转leetcode206
最近准备结束自己的科研生涯,准备要开始找工作了,准备在LEETCODE刷刷题...刷的前40题全部用python刷的,各种调包速度奇快,后被师哥告知这样没意义,于是准备开始回归C++,Python用的 ...
- Leetcode-206 Reverse Linked List
#206. Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...
- 每天一道LeetCode--206. Reverse Linked List
Reverse a singly linked list. package cn.magicdu; import cn.magicdu.extra.ListNode; public class _20 ...
- [Swift]LeetCode206. 反转链表 | Reverse Linked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- LeetCode-206.ReverseLinked List
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4-> ...
- leetcode206. 反转链表
1:迭代法 假设存在链表 1 → 2 → 3 → Ø,我们想要把它改成 Ø ← 1 ← 2 ← 3. 在遍历列表时,将当前节点的 next 指针改为指向前一个元素.由于节点没有引用其上一个节点,因此必 ...
随机推荐
- 团队-爬虫豆瓣top250项目-模块开发过程
项目托管平台地址:https://github.com/gengwenhao/GetTop250.git 开发模块功能: "get_info()单个页面的爬取"功能,开发时间:15 ...
- 周强 201771010141《面向对象程序设计(java)》第四周学习总结
实验目的与要求 (1) 理解用户自定义类的定义: (2) 掌握对象的声明: (3) 学会使用构造函数初始化对象: (4) 使用类属性与方法的使用掌握使用: (5) 掌握package和import语句 ...
- 6.移动端App安装包的测试用例
安装 安装手册是否规范,是否简洁,是否通俗易懂. 安装手册是否齐全,正确,有改动时,文档是否同步更新 直接复制安装程序到电脑上,能否正常安装 按安装手册给出的步骤进行安装,安装是否正确 查看在安装过程 ...
- CentOS版Linux系统上运行ASP.NET应用
一.安装: 1. 安装Apache Http Server yum install httpd2. 安装Mono yum install mono3. 安装Mono插件,用来处理ASP.NET请求 y ...
- 使用CSMA/CD协议一个计算题
题干: 首先计算一下A这个以太网所容许的最短的帧它的发送帧的长度时间为: (8(前同步码为8)+64(最短帧长))*8(单位转换b到B)=576比特 有关于单位转换: B是Byte的缩写,B就是Byt ...
- python 参数传递 传值还是传引用
个人推测结论: 可变对象传引用,不可变对象传值 python里的变量不同于c中地址储值模型 a=100 b=100 print(id(a),id(b),a==b,a is b) #8790877986 ...
- tcpkill,干掉tcp连接
场景 当我们需要在不重启服务的情况下断开某一个TCP长连接时,tcpkill工具就非常有用.比如我们要测试某个长连接断开后程序自动重连的情况. tcpkill安装 这个连接的作者改了一下tcpkill ...
- ajax 调用webservice 跨域问题
注意两点 1. 在webservice的config中加入这段位置 (注意不是调用webservice的webconfig中加入) <system.webServer> <! ...
- solr6.4.1搜索引擎(3)增量同步mysql数据库
尚未实现首次同步mysql数据库的,请参考我的另一篇文章http://www.cnblogs.com/zhuwenjoyce/p/6512378.html(solr6.4.1搜索引擎同步mysql数据 ...
- Pyhon文件的用途
Python1个文件2种用途: 当文件被当做脚本运行时 __name__='__main__'当模块被导入使用 __name__='spam' ==>等于模块名 假定spam代码如下 money ...