码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode常见题型——链表


    1.算法思路

    (单)链表是由节点和指针构成的数据结构,每个节点存有一个值,和一个指向下一个节点的指针,因此很多链表问题可以用递归来处理。

    不同于数组,链表并不能直接获取任意节点的值,必须要通过指针找到该节点后才能获取其值。同理,在未遍历到链表结尾时,我们也无法知道链表的长度,除非依赖其他数据结构储存长度。

    1. struct ListNode {
    2. int val;
    3. ListNode *next;
    4. ListNode(int x) : val(x), next(nullptr) {}
    5. };

    由于在进行链表操作时,尤其是删除节点时,经常会因为对当前节点进行操作而导致内存或指针出现问题。有两个小技巧可以解决这个问题:

    • 尽量处理当前节点的下一个节点而非当前节点本身
    • 建立一个虚拟节点(dummy node),使其指向当前链表的头节点,这样即使原链表所有节点全被删除,也会有一个dummy 存在,返回dummy->next 即可。

    在刷LeetCode 的时候,如果想要删除一个节点,可以直接进行指针操作而无需回收内存。实际做软件工程时,对于无用的内存,尽量显式回收或利用智能指针。

    2.常见题型

    LeetCode-206. Reverse Linked List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a singly linked list, reverse the list, and returnthe reversed list.https://blog.csdn.net/qq_15711195/article/details/126133049LeetCode-21. Merge Two Sorted Lists [C++][Java]_贫道绝缘子的博客-CSDN博客You are given the heads of two sorted linked listslist1andlist2. Merge the two lists in a onesortedlist. The list should be made by splicing together the nodes of the first two lists.https://blog.csdn.net/qq_15711195/article/details/126170115LeetCode-24. Swap Nodes in Pairs [C++][Java]_贫道绝缘子的博客-CSDN博客Given alinked list, swap every two adjacent nodes and return its head. You must solve the problem withoutmodifying the values in the list's nodes (i.e., only nodes themselves may be changed.)https://blog.csdn.net/qq_15711195/article/details/126189263LeetCode-160. Intersection of Two Linked Lists [C++][Java]_贫道绝缘子的博客-CSDN博客Given the heads of two singly linked-listsheadAandheadB, returnthe node at which the two lists intersect. If the two linked lists have no intersection at all, returnnull.https://blog.csdn.net/qq_15711195/article/details/126190342LeetCode-234. Palindrome Linked List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a singly linked list, returntrueif it is a palindrome.https://blog.csdn.net/qq_15711195/article/details/126190827LeetCode-82. Remove Duplicates from Sorted List II [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a sorted linked list,delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Returnthe linked listsortedas well.https://blog.csdn.net/qq_15711195/article/details/126191494?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126191494%22%2C%22source%22%3A%22qq_15711195%22%7D&ctrtid=5TT04LeetCode-83. Remove Duplicates from Sorted List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a sorted linked list,delete all duplicates such that each element appears only once. Returnthe linked listsortedas well.https://blog.csdn.net/qq_15711195/article/details/122394297LeetCode-328. Odd Even Linked List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a singly linked list, group all the nodes with odd indices together followed by the nodes with even indices, and returnthe reordered list. Thefirstnode is consideredodd, and thesecondnode iseven, and so on.https://blog.csdn.net/qq_15711195/article/details/126206743?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126206743%22%2C%22source%22%3A%22qq_15711195%22%7D&ctrtid=61ROWLeetCode-19. Remove Nth Node From End of List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a linked list, remove thenthnode from the end of the list and return its head.https://blog.csdn.net/qq_15711195/article/details/126220472?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126220472%22%2C%22source%22%3A%22qq_15711195%22%7D&ctrtid=KYQhULeetCode-148. Sort List [C++][Java]_贫道绝缘子的博客-CSDN博客Given theheadof a linked list, returnthe list after sorting it inascending order.https://blog.csdn.net/qq_15711195/article/details/126220508?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22126220508%22%2C%22source%22%3A%22qq_15711195%22%7D&ctrtid=Hxt2J

  • 相关阅读:
    【动态规划之完全背包问题】在实际问题中优化背包模型以及无效化情况的处理
    Docker实践笔记04:Tomcat环境DockerFile制作
    Day 87
    C# 正则表达式大全
    新疆维吾尔自治区工程系列建筑专业职称评审条件
    hive日常使用时忘记部分补充(不定时)
    如何在 Chrome 中设置HTTP服务器?
    【C语言】明解数组(1)
    mysql 安装问题 mariadb-libs is obsoleted by mysql-community-libs
    【考研】操作系统复习冲刺(2023年408)
  • 原文地址:https://blog.csdn.net/qq_15711195/article/details/126220532
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号