Leecode刷题之旅-C语言/python-206反转链表
/*
* @lc app=leetcode.cn id=206 lang=c
*
* [206] 反转链表
*
* https://leetcode-cn.com/problems/reverse-linked-list/description/
*
* algorithms
* Easy (58.89%)
* Total Accepted: 41.2K
* Total Submissions: 69.9K
* Testcase Example: '[1,2,3,4,5]'
*
* 反转一个单链表。
*
* 示例:
* 输入: 1->2->3->4->5->NULL
* 输出: 5->4->3->2->1->NULL
*
* 进阶:
* 你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
*
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* reverseList(struct ListNode* head) {
struct ListNode* p = head;
struct ListNode *q = NULL;
if(head==NULL||head->next==NULL)
{
return head;
}
while(p!=NULL){
struct ListNode *temp;
temp = p->next;
p->next=q;
q = p;
p = temp;
}
return q;
}
这里设置了三个listnode指针变量。 如果比较难理解的话画个图就会好懂很多。
-----------------------------------------------------------------------------------------------------
python:
#
# @lc app=leetcode.cn id=206 lang=python3
#
# [206] 反转链表
#
# https://leetcode-cn.com/problems/reverse-linked-list/description/
#
# algorithms
# Easy (58.89%)
# Total Accepted: 41.2K
# Total Submissions: 69.9K
# Testcase Example: '[1,2,3,4,5]'
#
# 反转一个单链表。
#
# 示例:
#
# 输入: 1->2->3->4->5->NULL
# 输出: 5->4->3->2->1->NULL
#
# 进阶:
# 你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
#
#
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
def reverseList(self, head: ListNode) -> ListNode:
if head is None:
return None
cur = head
pre = None
nxt = cur.next
while nxt:
cur.next = pre
pre = cur
cur = nxt
nxt = nxt.next
cur.next = pre
head = cur
return head
Leecode刷题之旅-C语言/python-206反转链表的更多相关文章
- Leecode刷题之旅-C语言/python-7.整数反转
/* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...
- Leecode刷题之旅-C语言/python-203移除链表元素
/* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...
- Leecode刷题之旅-C语言/python-83删除排序链表中的重复元素
/* * @lc app=leetcode.cn id=83 lang=c * * [83] 删除排序链表中的重复元素 * * https://leetcode-cn.com/problems/rem ...
- Leecode刷题之旅-C语言/python-1.两数之和
开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...
- Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符
/* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...
- Leecode刷题之旅-C语言/python-28.实现strstr()
/* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...
- Leecode刷题之旅-C语言/python-434 字符串中的单词数
/* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...
- Leecode刷题之旅-C语言/python-326 3的幂
/* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...
- Leecode刷题之旅-C语言/python-263丑数
/* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...
随机推荐
- 一、docker学习笔记——安装docker
系统win10 企业版 1.下载docker CE 2.安装.注意,由于docker 与Oracle VM VirtualBox 冲突,在windows平台上二者不可共存.你只能2选1!! 3.如果d ...
- Myeclipes连接Mysql数据库配置
相信大家在网站上也找到了许多关于myeclipes如何连接mysql数据库的解决方案,虽然每一步都按照他的步骤来,可到最后还是提示连接失败,有的方案可能应个人设备而异,配置环境不同导致.经过个人多方探 ...
- 【深入理解JAVA虚拟机】第三部分.虚拟机执行子系统.3.函数调用与执行
这章原名叫“虚拟机字节码执行引擎”,实际就是讲的函数如何调用和执行的. 1.概述 “虚拟机”是一个相对于“物理机”的概念,这两种机器都有代码执行能力, 其区别是物理机的执行引擎是直接建立在处理器. 硬 ...
- 如何删除Word 2010中的“向下箭头”
原文:https://jingyan.baidu.com/article/e75aca85552916142edac614.html 在日常办公中,如果从网站复制了一段文字,直接粘贴到Word中时,常 ...
- 自动下单tomcat版本问题
\xalan\xalan,jar找不到是因为spring boot 中使用的是tomcat8.5,从platform依赖进来的运行时环境是tomcat8,导致覆盖原来的依赖,在platform中移除S ...
- MyBatis(1)优点&介绍&工程
本次全部学习内容:MyBatisLearning 一:jabc的相关说明: jdbc编程步骤: 加载数据库驱动 创建并获取数据库链接 创建jdbc statement对象 设置sql语句 设置sql语 ...
- 面试准备——(三)Selenium面试题总结
一.Selenium基本知识 1. 什么是Selenium? Selenium是浏览器自动化工具,主要用来Web的自动化测试,以及基于Web的任务管理自动化.它支持的语言有:python.Java.r ...
- C# 实现Excel读取接口写入数据
[Route("adm/getInfo")] [HttpGet] public string GetInfo() { var types = typeof(GCP.Server.W ...
- 【SPJ6285 NGM2 - Another Game With Numbers】 题解
题目链接:https://www.luogu.org/problemnew/show/SP6285 唉好久之前校内模拟赛的题目 嘴上说着明白但是实现起来我的位运算太丑陋了啊! #include < ...
- centos安装hadoop(伪分布式)
在本机上装的CentOS 5.5 虚拟机, 软件准备:jdk 1.6 U26 hadoop:hadoop-0.20.203.tar.gz ssh检查配置 [root@localhost ~]# ssh ...