/*
* @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反转链表的更多相关文章

  1. Leecode刷题之旅-C语言/python-7.整数反转

    /* * @lc app=leetcode.cn id=7 lang=c * * [7] 整数反转 * * https://leetcode-cn.com/problems/reverse-integ ...

  2. Leecode刷题之旅-C语言/python-203移除链表元素

    /* * @lc app=leetcode.cn id=203 lang=c * * [203] 移除链表元素 * * https://leetcode-cn.com/problems/remove- ...

  3. Leecode刷题之旅-C语言/python-83删除排序链表中的重复元素

    /* * @lc app=leetcode.cn id=83 lang=c * * [83] 删除排序链表中的重复元素 * * https://leetcode-cn.com/problems/rem ...

  4. Leecode刷题之旅-C语言/python-1.两数之和

    开学后忙的焦头烂额(懒得很),正式开始刷leecode的题目了. 想了想c语言是最最基础的语言,虽然有很多其他语言很简单,有更多的函数可以用,但c语言能煅炼下自己的思考能力.python则是最流行的语 ...

  5. Leecode刷题之旅-C语言/python-387 字符串中的第一个唯一字符

    /* * @lc app=leetcode.cn id=387 lang=c * * [387] 字符串中的第一个唯一字符 * * https://leetcode-cn.com/problems/f ...

  6. Leecode刷题之旅-C语言/python-28.实现strstr()

    /* * @lc app=leetcode.cn id=28 lang=c * * [28] 实现strStr() * * https://leetcode-cn.com/problems/imple ...

  7. Leecode刷题之旅-C语言/python-434 字符串中的单词数

    /* * @lc app=leetcode.cn id=434 lang=c * * [434] 字符串中的单词数 * * https://leetcode-cn.com/problems/numbe ...

  8. Leecode刷题之旅-C语言/python-326 3的幂

    /* * @lc app=leetcode.cn id=326 lang=c * * [326] 3的幂 * * https://leetcode-cn.com/problems/power-of-t ...

  9. Leecode刷题之旅-C语言/python-263丑数

    /* * @lc app=leetcode.cn id=263 lang=c * * [263] 丑数 * * https://leetcode-cn.com/problems/ugly-number ...

随机推荐

  1. 【Leetcode】【Medium】Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  2. 【Leetcode】【Medium】Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  3. 杨氏矩阵C++实现

    何为杨氏矩阵?这个网上的介绍很多,下面给出杨氏矩阵搜索算法: #include <iostream> using namespace std; // 杨氏矩阵查找算法 ], int N, ...

  4. Can't create new folder in windows7

    First, please use System File Checker tool to troubleshoot(诊断) this issue. If the issue persists, im ...

  5. redis外网连接的一些坑

    前言 在使用阿里云和腾讯云的redis 可以减少很大的维护量.但是在我们的业务场景中遇到了一个情况,阿里和腾讯的redis均不支持外网访问.因此,正好帮人解决一个问题,就拿出来分享一下. 阿呆的故事 ...

  6. 在ubuntu16.04上安装eclipse

     在ubuntu16.04上安装eclipse 一.下载     首先我们需要安装jdk1.8及其以上,然后从官网:https://www.eclipse.org/downloads/上下载,需要注意 ...

  7. C++使用BOOST操作文件、目录

    开始使用 在BOOST库出现之前,C++对于文件和目录的操作,大都借助于UNIX提供的底层文件和目录接口,从使用角度来看,这些底层的操作不够友好.BOOST中filesystem库是一种可移植的文件系 ...

  8. luogu P2016 战略游戏

    嘟嘟嘟 树形dp水题啦. 刚开始以为和[SDOI2006]保安站岗这道题一样,然后交上去WA了. 仔细想想还是有区别的,一个是能看到相邻点,一个是能看到相邻边.对于第一个,可以(u, v)两个点都不放 ...

  9. update会锁表吗?

    update会锁表吗? 两种情况: 1.带索引 2.不带索引 前提介绍: 方式:采用命令行的方式来模拟 1.mysq由于默认是开启自动提交事务,所以首先得查看自己当前的数据库是否开启了自动提交事务. ...

  10. 【luogu P2296 寻找道路】 题解

    题目链接:https://www.luogu.org/problemnew/show/P2296 题意:给定起点终点,找一条从起点到终点的最短路径使路上的每个点都能有路径到达终点. 我们先反着建一遍图 ...