![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
链表
文章平均质量分 87
memcpy0
希望探索文理结合的自由之路。
展开
-
LeetCode 2807. 在链表中插入最大公约数【链表,迭代,递归】1279
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2024-01-07 03:50:30 · 995 阅读 · 0 评论 -
LeetCode 92. Reverse Linked List II【链表,头插法】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-09-08 00:45:36 · 947 阅读 · 0 评论 -
LeetCode 725. Split Linked List in Parts【链表】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-09-07 23:40:25 · 363 阅读 · 0 评论 -
LeetCode 138. Copy List with Random Pointer【链表,DFS,迭代,哈希表】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-09-05 14:45:28 · 821 阅读 · 0 评论 -
LeetCode 1171. Remove Zero Sum Consecutive Nodes from Linked List【链表,递归;哈希表,前缀和】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-18 11:28:57 · 97 阅读 · 0 评论 -
LeetCode 430. Flatten a Multilevel Doubly Linked List【链表/DFS/递归/迭代】中等
You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. These child lists may have one or more children of their own, and so on, to原创 2021-09-28 10:16:19 · 306 阅读 · 0 评论 -
洛谷 UVA11988 破损的键盘 Broken Keyboard (a.k.a. Beiju Text)【链表】
题目描述输入格式输出格式题意翻译你在输入文章的时候,键盘上的Home键和End键出了问题,会不定时的按下。你却不知道此问题,而是专心致志地打稿子,甚至显示器都没开。当你打开显示器之后,展现你面前的数一段悲剧文本。你的任务是在显示器打开前计算出这段悲剧的文本。 给你一段按键的文本,其中’[‘表示Home键,’]'表示End键,输入结束标志是文件结束符(EOF)。输出一行,即这段悲剧文本。 翻译贡献者UID:71371输入输出样例输入 #1This_is_a_[Beiju]_text[[原创 2021-05-21 20:05:08 · 253 阅读 · 1 评论 -
洛谷 UVA101 The Blocks Problem【模拟/链表】
输入格式输出格式题意翻译初始时从左到右有 nnn 个木块,编号为 0…n−10 \ldots n-10…n−1 ,要求实现下列四种操作:move a onto b : 把 a 和 b 上方的木块归位,然后把 a 放到 b 上面。move a over b : 把 a 上方的木块归位,然后把 a 放在 b 所在木块堆的最上方。pile a onto b : 把 b 上方的木块归位,然后把 a 及以上的木块坨到 b 上面。pile a over b : 把 a 及以上的木块坨到 b 的上面。原创 2021-05-19 04:07:35 · 445 阅读 · 1 评论 -
LeetCode C++ 725. Split Linked List in Parts【Linked List】中等
Given a (singly) linked list with head node root, write a function to split the linked list into k consecutive linked list "parts".The length of each part should be as equal as possible: no two parts should have a size differing by more than 1. This may原创 2021-03-14 22:18:23 · 187 阅读 · 0 评论 -
LeetCode C++ 25. Reverse Nodes in k-Group【Linked List/Stack】困难
Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the原创 2020-12-04 16:15:04 · 271 阅读 · 0 评论 -
LeetCode 142. Linked List Cycle II【链表,哈希表;双指针】
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally,原创 2020-11-25 22:18:41 · 176 阅读 · 0 评论 -
LeetCode C++ 面试题 02.06. Palindrome Linked List LCCI【Linked List/Recursion】简单
Implement a function to check if a linked list is a palindrome.Example 1:Input: 1->2Output: false Example 2:Input: 1->2->2->1Output: true Follow up: Could you do it in O(n) time and O(1) space?题意:编写一个函数,检查输入的链表是否是回文的。解法1 辅助空间使用原创 2020-11-23 23:06:37 · 154 阅读 · 0 评论 -
LeetCode C++ 160. Intersection of Two Linked Lists【Linked List/Two Pointers】简单
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:begin to intersect at node c1.Example 1:Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skip原创 2020-11-17 01:10:45 · 155 阅读 · 0 评论 -
LeetCode C++ 328. Odd Even Linked List【Linked List】中等
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in place. The program should run in O(1) space complexity and原创 2020-11-14 22:09:52 · 164 阅读 · 0 评论 -
LeetCode C++ 876. Middle of the Linked List【Linked List/Two Pointers】简单
Given a non-empty, singly linked list with head node head, return a middle node of linked list.If there are two middle nodes, return the second middle node.Example 1:Input: [1,2,3,4,5]Output: Node 3 from this list (Serialization: [3,4,5])The returned原创 2020-11-14 13:35:42 · 155 阅读 · 0 评论 -
LeetCode 剑指 Offer 22. 链表中倒数第k个节点【链表/双指针】简单
输入一个链表,输出该链表中倒数第 k 个节点。为了符合大多数人的习惯,本题从 1 开始计数,即链表的尾节点是倒数第 1 个节点。例如,一个链表有 6 个节点,从头节点开始,它们的值依次是 1、2、3、4、5、6 。这个链表的倒数第 3 个节点是值为 4 的节点。示例:给定一个链表: 1->2->3->4->5, 和 k = 2.返回链表 4->5.解法 快慢指针class Solution {public: ListNode* getKthFromEn原创 2020-11-14 10:16:21 · 162 阅读 · 0 评论 -
LeetCode C++ 剑指 Offer 25. 合并两个排序的链表【Linked List/Recursion】简单
输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是递增排序的。示例1:输入:1->2->4, 1->3->4输出:1->1->2->3->4->4限制: 0 <= 链表长度 <= 1000解法1 迭代归并class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (!l1 || !l2)原创 2020-11-13 19:50:07 · 162 阅读 · 0 评论 -
LeetCode C++ 剑指 Offer 24. 反转链表【Linked List】简单
定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。示例:输入: 1->2->3->4->5->NULL输出: 5->4->3->2->1->NULL限制: 0 <= 节点个数 <= 5000解法1 递归class Solution {public: ListNode* reverseList(ListNode* head) { if (head == nullptr ||原创 2020-11-13 14:09:33 · 151 阅读 · 0 评论 -
LeetCode C++ 1290. Convert Binary Number in a Linked List to Integer【Linked List】简单
Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.Return the decimal value of the number in the linked list.Example 1:In原创 2020-11-11 23:07:43 · 224 阅读 · 0 评论 -
LeetCode 445. Add Two Numbers II【链表,栈】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2020-11-04 20:09:09 · 159 阅读 · 0 评论 -
LeetCode C++ 234. Palindrome Linked List【链表/双指针/递归】简单
Given a singly linked list, determine if it is a palindrome.Example 1:Input: 1->2Output: falseExample 2:Input: 1->2->2->1Output: trueFollow up: Could you do it in O(n)O(n)O(n) time and O(1)O(1)O(1) space?题意:判断一个单链表是否是回文链表。解法1 复制链表值原创 2020-10-23 15:48:32 · 232 阅读 · 0 评论 -
LeetCode C++ 61. Rotate List【Linked List/Two Pointers】中等
Given a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1->2->3->4->5->NULL, k = 2Output: 4->5->1->2->3->NULLExplanation:rotate 1 steps to the right: 5->1->2->3->原创 2020-10-22 20:29:27 · 157 阅读 · 0 评论 -
LeetCode C++ 143. Reorder List【LinkedList/Deque】中等
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You may not modify the values in the list's nodes, only nodes itself may be changed.Example 1:Given 1->2->3->4, reorder it to 1->4->2->3.Example 2:G原创 2020-10-21 22:01:33 · 232 阅读 · 0 评论 -
LeetCode C++ 19. Remove Nth Node From End of List【链表/双指针】中等
Given the head of a linked list, remove the nth node from the end of the list and return its head.Follow up: Could you do this in one pass?Example 1:Input: head = [1,2,3,4,5], n = 2Output: [1,2,3,5]Example 2:Input: head = [1], n = 1Output: []Exa原创 2020-10-18 10:14:08 · 148 阅读 · 0 评论 -
LeetCode 141. Linked List Cycle【链表,双指针】简单
Given head , the head of a linked list, determine if the linked list has a cycle in it.There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to den原创 2020-10-09 11:28:46 · 164 阅读 · 0 评论 -
LeetCode C++ 148. Sort List【链表/排序】中等
Given the head of a linked list, return the list after sorting it in ascending order.Follow up: Can you sort the linked list in O(n logn) time and O(1) memory (i.e. constant space)?Example 1:Input: head = [4,2,1,3]Output: [1,2,3,4]Example 2:Input: h原创 2020-10-07 22:12:47 · 165 阅读 · 0 评论 -
LeetCode C++ 147. Insertion Sort List【排序/链表/递归】中等
Sort a linked list using insertion sort.A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list.With each iteration one element (red) is removed from the input data and inserted in-pl原创 2020-09-17 19:15:28 · 154 阅读 · 0 评论 -
LeetCode C++ 21. Merge Two Sorted Lists【链表】简单
Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Output: 1->1->2->3->4->4题意:将两个升序链表合并为一个新的升序链表原创 2020-09-17 15:34:15 · 150 阅读 · 0 评论 -
LeetCode 237. Delete Node in a Linked List【链表】简单
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Given linked list – head = [4,5,1,9] , which looks like following:Example 1:Input: head = [4,5,1,9], node = 5Output: [4,1,9]Explanation: You a原创 2020-08-23 15:37:18 · 304 阅读 · 0 评论 -
LeetCode C++ 24. Swap Nodes in Pairs【链表】中等
Given a linked list, swap every two adjacent nodes and return its head.You may not modify the values in the list’s nodes, only nodes itself may be changed.Example:Given 1->2->3->4, you should return the list as 2->1->4->3.题意:给定一个链表,两原创 2020-08-23 14:50:13 · 223 阅读 · 0 评论 -
LeetCode C++ 203. Remove Linked List Elements【链表】简单
Remove all elements from a linked list of integers that have value val.Example:Input: 1->2->6->3->4->5->6, val = 6Output: 1->2->3->4->5题意:从一个单链表中删除所有等于给定值的结点。思路1:迭代删除+无虚拟头结点。代码:class Solution {public: ListNode*原创 2020-08-23 13:15:07 · 187 阅读 · 0 评论 -
LeetCode 206. 反转链表, LCR 024. 反转链表【链表,递归】简单
Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively or recursively. Could you implement both?题意:递归或者迭代地翻转一个单链表。思路1:迭代翻转单原创 2020-08-23 00:23:49 · 301 阅读 · 1 评论 -
LeetCode C++ 109. Convert Sorted List to Binary Search Tree【DFS/Linked List】中等
Given the head of a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never原创 2020-08-19 22:42:56 · 215 阅读 · 0 评论 -
LeetCode C++ 面试题 02.01. Remove Duplicate Node LCCI【Linked List】简单
Write code to remove duplicates from an unsorted linked list.Example1: Input: [1, 2, 3, 3, 2, 1] Output: [1, 2, 3]Example2: Input: [1, 1, 1, 1, 2] Output: [1, 2]Note:The length of the list is within the range [0, 20000].The values of the list e原创 2020-06-26 14:56:35 · 226 阅读 · 0 评论 -
LeetCode C++ 2. Add Two Numbers【LinkedList】中等
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i...原创 2019-11-20 21:21:45 · 200 阅读 · 0 评论 -
【数据结构Java实现】链表和递归
文章目录一、递归思想介绍二、链表的天然递归结构性质三、推而广之四、总结递归是极其重要的组建程序逻辑的一种方式,是作为程序员,编写计算机程序,学习计算机科学绕不过的一个话题。后面的【数据结构Java实现】都和递归脱不开关系。我不会将递归作为算法的一个专栏,因为递归实际上是编写程序的一种结构,和循环是等价的。递归真正难的地方,在于与算法思想相结合——如排序,分治,搜索,图论算法,动态规划等;在于与...原创 2020-03-12 18:20:32 · 385 阅读 · 0 评论 -
LeetCode C++ 203. Remove Linked List Elements【LinkedList】【递归】
文章目录写法一 统一处理写法二 分开处理两种情况写法三 使用虚拟头结点统一操作写法四 递归删除所有值为val的结点Remove all elements from a linked list of integers that have value val.Example:Input: 1->2->6->3->4->5->6, val = 6Output...原创 2020-03-12 16:21:52 · 361 阅读 · 0 评论 -
【数据结构Java实现】单链表【最基本的动态数据结构】
文章目录一、链表LinkedList二、无头单链表实现1. 基本的方法2. 添加元素三、带头单链表的实现前面的所谓动态数组,实际上是靠resizeresizeresize这种操作实现的,是对用户来说的。而链表,是真正的动态数据结构,也是最简单的动态数据结构(更难的有二叉搜索树,trie树,红黑树等)。学习链表,可以让我们更加深入的理解Java的引用/C++的指针;而且链表是递归数据结构,链表的...原创 2020-03-12 00:44:39 · 225 阅读 · 0 评论 -
LeetCode C++ 206. Reverse Linked List【LinkedList】【链表反转】
Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively or recursively...原创 2020-02-23 21:24:17 · 246 阅读 · 0 评论