当前位置: 首页 > news >正文

【Hot100】LeetCode—206. 反转链表

目录

  • 1- 思路
    • 递归法
  • 2- 实现
    • ⭐206. 反转链表——题解思路
  • 3- ACM 实现


  • 原题连接:206. 反转链表

1- 思路

递归法

  • 递归三部曲
    • ①终止条件:遇到 head ==null || head.next==null 的时候
    • ②递归逻辑:定义 curcur 执行递归逻辑,也就是调用 当前reverse(cur.next)
      • head.next.next = head;
      • head.next = null;

2- 实现

⭐206. 反转链表——题解思路

在这里插入图片描述

class Solution {public ListNode reverseList(ListNode head) {if(head == null || head.next==null){return head;}// 递归ListNode cur = reverseList(head.next);head.next.next = head;head.next = null;return cur;}
}

3- ACM 实现

public class reverseList {public static class ListNode {int val;ListNode next;ListNode(int x) {val = x;next = null;}}public static ListNode reverseList(ListNode head){if(head == null|| head.next == null){return head;}ListNode cur = reverseList(head.next);head.next.next = head;head.next = null;return cur;}public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("输入链表长度");int n = sc.nextInt();ListNode head = null,tail=null;for(int i = 0 ; i < n;i++){ListNode nowNode  = new ListNode(sc.nextInt());if(head==null){head = nowNode;tail = nowNode;}else{tail.next = nowNode;tail = nowNode;}}ListNode forRes = reverseList(head);while(forRes!=null){System.out.print(forRes.val+" ");forRes = forRes.next;}}
}

http://www.mrgr.cn/news/3513.html

相关文章:

  • Linux系统-打包重定向/管道符/进程shell脚本
  • C语言文件操作
  • 【秋招笔试】8.18大疆秋招(第一套)-后端岗
  • 作品集图片美化处理网站推荐
  • 初识C++:开启C++之旅
  • redis string类型
  • CSS的:definite和:indefinitte伪类:探索确定性与不确定性的元素选择
  • InputApc()函数是如何调用ProcessKeyboardInput()函数的?
  • Android 架构模式之 MVP
  • Linux --- 文件系统
  • reactive 和 ref 的区别和联系
  • 二十二、状态模式
  • 【Orb-Slam3学习】 ORBextractor类主要成员函数调用关系
  • windows C++- Com技术简介(上)
  • MySql高级视频笔记
  • csdn狗都不用-测试再次解释
  • 共享内存及网络通信
  • 白骑士的计算机名词解析之各种“面向”
  • 【QT文件操作】---xml文件读取
  • 火山引擎AI创新巡展:豆包比友商便宜98%,行业落地探索,2000人座无虚席,PPT值得拍照收藏