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 ...
随机推荐
- kivy.org - Open source Python library for rapid development of applications
kivy.org - Open source Python library for rapid development of applicationsthat make use of innovati ...
- 使用 Sinamics S120 驱动脚本配置扩展报文
为了传输故障代码.电流.温度等信息.通常需要使用扩展报文的方式来发送这些信息.在驱动数量较少的情况下,可以进行手动配置. 如果驱动数量很多,可以使用脚本script的方式来配置扩展报文. 驱动编号 注 ...
- July 04th 2017 Week 27th Tuesday
Nothing is really beautiful but truth. 只有真理才是真美. Truth can be beautiful, but it also can be cruel. W ...
- 【转】[C++]实现委托模型
原文地址:http://www.cnblogs.com/zplutor/archive/2011/09/17/2179756.html 我对.Net的委托模型印象很深刻,使用委托,可以快速实现观察者模 ...
- easyui学习笔记6—基本的Accordion(手风琴)
手风琴也是web页面中常见的一个控件,常常用在网站后台管理中,这里我们看看easyui中基本的手风琴设置. 1.先看看引用的资源 <meta charset="UTF-8" ...
- oracle 函数 WMSYS.WM_CONCAT 与对应 mysql 函数GROUP_CONCAT的用法
1.oracle函数 WMSYS.WM_CONCAT的使用 如图,将图1的结果变成图2的结果,使用函数 WMSYS.WM_CONCAT 即可. 查询语句需要配合使用group by select aa ...
- 前端面试题总结(一)HTML篇
前端面试题总结(一)HTML篇 一.iframe的优缺点? 缺点: 1.会阻塞主页面的onload事件(iframe和主页面共享链接池,而浏览器对相同域的链接有限制,所以会影响页面的并行加载). 解决 ...
- hdu-3074 Multiply game---线段树+单点更新
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3074 题目大意: 给一些数,进行点更新或者是区间计算乘积 解题思路: 裸的线段树,注意空间开大点 # ...
- PowerDNS简单教程(3):管理篇
目录: PowerDNS简单教程(1):安装篇 http://www.cnblogs.com/anpengapple/p/5205130.html PowerDNS简单教程(2):功能篇 http:/ ...
- 系统监控zabbix
zabbix(音同 zæbix)是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供灵活的通知机制以让系统 ...