首页 > 其他分享 >LeetCode 206. 反转链表

LeetCode 206. 反转链表

时间:2022-08-24 10:14:00浏览次数:71  
标签:head ListNode val 206 int res next 链表 LeetCode

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode* res = new ListNode(0);
        ListNode* temp = NULL;

        while (head) {
            temp = head ->next;
            head ->next = res ->next;
            res ->next = head;
            head = temp;
        } 

        return res ->next;
    }
};

标签:head,ListNode,val,206,int,res,next,链表,LeetCode
From: https://www.cnblogs.com/hjy94wo/p/16618861.html

相关文章

  • leetcode 热题100刷题-二叉树的中序遍历
    题题号:94题目:二叉树的中序遍历难度:简单链接:https://leetcode.cn/problems/binary-tree-inorder-traversal/2022/08/23答案算法思路  本题在课程中是学过的。  ......
  • [Oracle] LeetCode 696 Count Binary Substrings
    Givenabinarystrings,returnthenumberofnon-emptysubstringsthathavethesamenumberof0'sand1's,andallthe0'sandallthe1'sinthesesubstring......
  • 链表冒泡排序
    publicListNodesortList(ListNodehead){ListNodeheadpre=newListNode();headpre.next=head;intlength=0;while(head!=null){......
  • 算法:两个链表的第一个公共节点
    问题输入两个链表,找出它们的第一个公共节点。解决//1、暴力解法classSolution{ListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB){w......
  • 「数据结构与算法」链表 —— 看这一篇就够了(超详细)
     一、链表简介链表是一种物理存储单元上非连续、非顺序的存储结构,数据结构的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点......
  • leetcode97-交错字符串
    交错字符串dp定义一个二维的dp数组,表示s1选取i个字符和s2选取j个字符组成s3的前i+j个字符能否成立。dp递归方程:如果i==0&&j==0,表示没有任何字符,true如......
  • leetcode 697. Degree of an Array 数组的度(简单)
    一、题目大意https://leetcode.cn/problems/degree-of-an-array给定一个非空且只包含非负数的整数数组nums,数组的度的定义是指数组里任一元素出现频数的最大值。你的......
  • leetcode160:相交链表
    /***题目:*给你两个单链表的头节点headA和headB,请你找出并返回两个单链表相交的起始节点。*如果两个链表不存在相交节点,返回null。*注意......
  • LeetCode 541. 反转字符串 II
    思路:每次移动2k位,判断是否超过数组,超过则全部反转,没超过则反转到第i+k个classSolution{public:stringreverseStr(strings,intk){for(inti=0;......
  • LeetCode - 三数之和
    题目信息源地址:三数之和给你一个包含n个整数的数组nums,判断nums中是否存在三个元素a,b,c,使得a+b+c=0,请你找出所有和为0且不重复的三元组。注意:答案中不可......