LeetCode常见题型——链表

1.算法思路

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

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

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(nullptr) {}
};

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

  • 尽量处理当前节点的下一个节点而非当前节点本身
  • 建立一个虚拟节点(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

注意:

1. 双指针解题时

  • 判别条件为fast != null && fast.next != null,slow最终踏在中位(奇)或右半边起点(偶);
  • 判别条件为fast.next != null && fast.next.next != null, slow最终踏在中位或左半边终点
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

贫道绝缘子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值