链表
FrancisGeek的工作室
算法&设计模式
展开
-
Delete Node in a Linked List
给定某结点,删除某结点原创 2016-12-01 21:00:49 · 150 阅读 · 0 评论 -
Insertion Sort List
Description:Sort a linked list using insertion sort.问题描述:用插入排序给一条链表元素排序解法一:思路:将链表元素一个一个的切下来,利用两个指针,一个排好序链表的表头指针(sortedHead),一个排好序链表的表尾指针。 插入结点时分为三种情况: + 待插入元素比新表表头元素小 + 待插入元素比新表表尾大 + 待插入元素在新表表头和新表表原创 2016-12-04 21:46:55 · 139 阅读 · 0 评论 -
Linked List Cycle II
Description:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.问题描述:给定一条链表,判断是否有环,函数返回环开始的第一个结点(也就是环的入口处)解法一:思路:这个问题算是之前判断链表是否有环问题的一个拓展。解决这个问题的思路分为两步,第一步是个追击原创 2016-12-05 18:57:15 · 148 阅读 · 0 评论 -
Partition List
Description: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 i原创 2016-12-05 19:20:03 · 141 阅读 · 0 评论 -
Remove Duplicates from Sorted List II
Description:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.问题描述:给一条已经排好序的链表,删除所有重复结点,只保留在链表中出现过一次的结点。Ex:Given 1->2->3->3-原创 2016-12-05 19:30:22 · 134 阅读 · 0 评论 -
Odd Even Linked List
Description: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原创 2016-12-06 22:30:08 · 291 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree
Description:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.问题描述:给一条已经排好序的单链表(元素次序按升序排列),将其转化为等价的二叉搜索树(Binary Search Tree ).二叉搜索树:这里要明白二叉搜索树原创 2016-12-06 22:44:45 · 176 阅读 · 0 评论 -
Remove Duplicates from Sorted List
Description:Given a sorted linked list, delete all duplicates such that each element appear only once.问题描述:给一条已经排好序的链表,删除其中的重复值元素,使每个元素仅出现一次。输出给定链表的头结点,返回处理好链表的头结点。解法一:思路:先进行边界条件检查,处理空链表和单结点链表的情况。后面对链表原创 2016-12-02 22:46:51 · 133 阅读 · 0 评论 -
链表反转总结
Description:Reverse a linked list from position m to n. Do it in-place and in one-pass.Ex:Given 1->2->3->4->5, m = 2 and n = 4 return 1->4->3->2->5问题描述:在给定的区间反转链表1 ≤ m ≤ n ≤ length of list.解法一:思路:这里需要原创 2016-12-05 19:08:59 · 160 阅读 · 0 评论 -
Reorder List
Description:Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…问题描述:给一条单链表,有n+1个结点,将第n个结点插入到第0个结点与第1个结点之间,将第n-1个结点插入到第1个结点与第2个结点之间,依次类推。Ex:Given {1,2,3,4}, reorder it原创 2016-12-04 13:03:36 · 211 阅读 · 0 评论 -
Sort List
Description:Sort a linked list in O(n log n) time using constant space complexity问题描述给一条给定链表中的元素排序,返回排好序链表的头结点限定条件时间复杂度为O(nlogn) , 空间复杂度为O(1)解法一:思路:看到时间复杂度为O(nlogn) , 应该想到归并排序和快排,这里给出归并排序的思路。 第一步:用快指原创 2016-12-04 12:51:50 · 236 阅读 · 0 评论 -
Rotate List
Description:Given a list, rotate the list to the right by k places, where k is non-negative.Ex:Given 1->2->3->4->5 and k = 2 return 4->5->1->2->3问题描述:给一条链表,给一个非负的k值,将链表向右旋转k个地方(题目原文直译,简直不是人话)。通俗点的意思是,原创 2016-12-04 12:06:03 · 238 阅读 · 0 评论 -
Remove Linked List Elements
删除单链表中的指定元素原创 2016-12-01 21:19:30 · 164 阅读 · 0 评论 -
Remove Nth Node From End of List
删除链表最后开始的第N个指针原创 2016-12-02 22:24:51 · 149 阅读 · 0 评论 -
Swap Nodes in Pairs
Description:Given a linked list, swap every two adjacent nodes and return its head.问题描述:给一条单链表(头结点),对链表中两两相邻的元素进行对调,返回处理过链表的头结点Ex:Given 1->2->3->4, you should return the list as 2->1->4->3.问题限定:空间复杂度为O原创 2016-12-02 22:39:25 · 140 阅读 · 0 评论 -
Merge Two Sorted Lists(合并两个排序链表)
Description:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.问题描述:合并两条已经排好序的链表,新的链表也要保持有序。输入原始链表的头结点,返回合并链表的头原创 2016-12-02 22:54:52 · 161 阅读 · 0 评论 -
Intersection of Two Linked Lists
Description:Write a program to find the node at which the intersection of two singly linked lists begins.问题描述:写一个函数,返回两条链表交集链表的头结点。限制:整条链表中均无环时间复杂度O(n),空间复杂度O(1)解法一(迭代)思路:两次迭代操作,假设有A和B两条链表,对于每个链表每次遍原创 2016-12-02 23:07:28 · 159 阅读 · 0 评论 -
Linked List Cycle
Description:Given a linked list, determine if it has a cycle in it.问题描述:检测给定链表是否有环限定条件:空间复杂度O(1),不开辟额外空间解法一:思路:先进行空链表检查,循环问题可以看成周期性问题,如果结点总数为n,一个指针走n步必定和一个指针走kn部相遇(如果链表有环),这里为了简化另k=2。所以可以设定两个指针,一个walke原创 2016-12-03 10:12:59 · 173 阅读 · 0 评论 -
Palindrome Linked List
Description:Given a singly linked list, determine if it is a palindrome问题描述:给条单链表,判断是否为回文链表回文:正读反读都一样Ex:1->2->3->2->1—–回文链表 1->2->2->1—-回文链表 1->3->2—-非回文链表限定条件:时间复杂度O(n) ,空间复杂度O(1)解法一:思路:第一步:用两个指针(快指原创 2016-12-04 11:31:30 · 198 阅读 · 0 评论 -
Add Two Numbers
Description:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i原创 2016-12-04 11:43:38 · 208 阅读 · 0 评论 -
链表数据结构特点
引言最近在重温链表的算法题,发现很久没做,竟毫无思路。其中的一些基本套路都已经忘了,其实我想到其实是自己没有总结链表这个数据结构的一些tricks.链表数据结构的特点为了简便,这里以单链表为例子说明,为了便于读者理解,这里和数组作为对比。在数组中,是通过索引访问元素的,但是不要小看索引,好多精妙的算法都是利用了这个索引玩花样。但是链表不能,只能通过链一个一个的找。这个说明了链表的优势不在快速找到元素原创 2017-11-14 11:25:36 · 15896 阅读 · 0 评论