链表
涛涛酱
北邮研究僧在读
展开
-
二叉树转为双向链表
代码: /* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: TreeNode* Convert(TreeNode* root) { if(root==NULL) return NULL;//递转载 2020-10-13 10:42:10 · 229 阅读 · 0 评论 -
【LeetCode 445】 Add Two Numbers II
题目描述 两个链表,表示数字,高位在首部。 求两个数字之和。 思路 用栈。 或者反转链表。 代码 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * Lis原创 2020-09-07 20:52:39 · 94 阅读 · 0 评论 -
【LeetCode 86】Partition List
题目描述 Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. Example: Input: head = 1->4-&g原创 2020-05-17 13:35:55 · 62 阅读 · 0 评论 -
【LeetCode 92】Reverse Linked List II
题目描述 Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 思路 设了一个头结点,m==1时,方便。 代码 /** * Definition for原创 2020-05-16 23:00:59 · 66 阅读 · 0 评论 -
【面试题 & LeetCode 25】Reverse Nodes in k-Group
题目描述 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...原创 2020-04-20 21:53:49 · 134 阅读 · 0 评论 -
【面试题 & LeetCode 148】单链表排序
插入排序 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNo...原创 2020-04-19 22:33:43 · 160 阅读 · 0 评论 -
【LeetCode 143】Reorder List
题目描述 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...原创 2020-04-15 17:25:05 · 93 阅读 · 0 评论 -
【LeetCode 83】Remove Duplicates from Sorted List
题目描述 Given a sorted linked list, delete all duplicates such that each element appear only once. Example 1: Input: 1->1->2 Output: 1->2 Example 2: Input: 1->1->2->3->3 Output: 1-&...原创 2020-04-09 18:20:02 · 64 阅读 · 0 评论 -
【LeetCode 206】 Reverse Linked List
题目描述 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursi...原创 2020-02-13 22:12:43 · 54 阅读 · 0 评论 -
【LeetCode 208】 Implement Trie (Prefix Tree)
题目描述 Implement a trie with insert, search, and startsWith methods. Example: Trie trie = new Trie(); trie.insert("apple"); trie.search("apple"); // returns true trie.search("app"); // returns fa...原创 2020-02-12 14:13:26 · 61 阅读 · 0 评论 -
【LeetCode 341】Flatten Nested List Iterator
题目描述 Given a nested list of integers, implement an iterator to flatten it. Each element is either an integer, or a list – whose elements may also be integers or other lists. Example 1: Input: [[1,1],2...原创 2020-02-12 12:40:57 · 91 阅读 · 0 评论 -
【LeetCode 160】 Intersection of Two Linked Lists
题目描述 Write a program to find the node at which the intersection of two singly linked lists begins. Notes: If the two linked lists have no intersection at all, return null. The linked lists must retain...原创 2020-02-07 18:34:22 · 77 阅读 · 0 评论 -
【LeetCode 148】 Sort List
题目描述 Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2->1->3 Output: 1->2->3->4 Example 2: Input: -1->5->3->4->0 Output: -1-&g...原创 2020-02-07 13:18:34 · 76 阅读 · 0 评论 -
【LeetCode 138】 Copy List with Random Pointer
题目描述 A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. The Linked List is represented ...原创 2020-02-06 22:32:11 · 90 阅读 · 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. To represent a cycle in the given linked list, we use an integer pos which represents the position ...原创 2020-02-01 21:58:34 · 74 阅读 · 0 评论 -
【LeetCode 287】 Find the Duplicate Number
题目描述 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate numbe...原创 2020-02-01 15:12:35 · 74 阅读 · 0 评论 -
【剑指 offer】反转链表
题目描述 输入一个链表,反转链表后,输出新链表的表头。 思路 和指针有关的问题,要注意判断边界条件和特殊样例。 可以用递归和非递归两种方式。 注意解除原链表头节点的nxt关系,否则会死循环。 代码 递归: struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { }...原创 2019-12-02 11:23:03 · 58 阅读 · 0 评论 -
【剑指 offer】删除链表中重复的节点
题目描述: 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5。 思路: 先写了一个非递归版,对链表遍历一遍可以解决。先找到头节点,然后对当前节点和前一个、后一个节点的值比较,如果不同,可以加入。 写完发现。嚯,别原创 2019-03-10 20:00:39 · 88 阅读 · 0 评论 -
【剑指 offer】链表中环的入口结点
题目描述: 给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。 思路: 判断是否存在环: 使用追赶的方法,设定两个指针,均从头指针开始,每次分别前进1步、2步。如存在环,则两者相遇;如不存在环,遇到NULL。 找入口: 当fast指针等于slow指针时,slow指针肯定还没有遍历完整个链表,而此时fast指针已经在环内循环了n圈(n>=1),假定从链表头指针开始s...原创 2019-03-10 21:22:21 · 66 阅读 · 0 评论 -
【LeetCode 328】 Odd Even 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...原创 2019-03-07 19:03:42 · 79 阅读 · 0 评论 -
【剑指 offer】复杂链表的复制
题目描述: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点),返回结果为复制后复杂链表的head。(注意,输出结果中请不要返回参数中的节点引用,否则判题程序会直接返回空) 思路: …不太懂这个不要用引用的要求。疯狂返回空,好像是不用random的复制和引用? 代码: struct RandomListNode { int label;...原创 2019-04-19 11:01:13 · 94 阅读 · 0 评论 -
【LeetCode 234】Palindrome Linked List 待解决
题目描述: 给一个单链表,判断是否是回文的。 要求时间复杂度:O(n),空间复杂度:O(1)。 思路: 反转一半链表,然后依次比较。 但是不知道为什么提交RE,,,返回的case都测过了。。。。 代码: class Solution { public: bool isPalindrome(ListNode* head) { if (head == NULL || head-...原创 2019-05-15 16:01:25 · 76 阅读 · 0 评论 -
【LeetCode 23.】Merge k Sorted Lists
题目大意: 给出 n 个有序链表,合并为1个有序链表。 思路1: 优先队列。每次取n个链表的最小值,然后把取出节点的next节点加入队列。 思路2: 每次合并2个。时间复杂度不太会计算。 链表除了操作麻烦还是和数组不一样的,有自己的特性。这道题感觉出来了。 代码: 代码1: /** * Definition for singly-linked list. * struct ListNode {...原创 2019-06-27 10:46:24 · 72 阅读 · 0 评论 -
【剑指offer】重建二叉树
题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 思路: 根据前序在中序中找到根节点,然后递归对左右子树创建。 代码: struct TreeNode { int val; TreeN...原创 2019-03-02 20:22:05 · 87 阅读 · 0 评论