
ACM-链表
文章平均质量分 75
LarryNLPIR
专注NLP/IR/Machine Learning/Data Mining
展开
-
LeetCode Sort List
Sort a linked list in O(n log n) time using constant space complexity.思路分析:这题要求在O(n log n) 和常量空间对单链表排序,O(n log n) 的排序算法有快速排序,归并排序和堆排序,对于快速排序,其最坏情况下的时间复杂读是O(n),所以不符合要求。我们可以用归并排序解决这题。用归并排序的好处是平均时间和最坏时间都原创 2015-01-27 13:29:49 · 2169 阅读 · 1 评论 -
LeetCode Merge Two Sorted Lists
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.思路分析:这题是 LeetCode Sort List需要使用的merge两个有序链表使得结果仍然有序这个子过程,详细原创 2015-01-27 13:43:31 · 1944 阅读 · 0 评论 -
LeetCode Linked List Cycle
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路分析:这题很简单,直接用快慢指针法就可以解决,快指针速度是慢指针速度的2倍,如果有环,它们一定会相遇。AC Code/** * Definition for singly-lin原创 2015-01-19 14:47:40 · 1327 阅读 · 0 评论 -
LeetCode 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.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.You原创 2015-01-19 14:28:33 · 3195 阅读 · 0 评论 -
LeetCode Reverse Linked List II
Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy the following condi原创 2015-01-19 14:36:55 · 1833 阅读 · 0 评论 -
LeetCode Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?思路分析:这题比较容易想到的解法是先检查是否有环,然后检查每个node是否在环内,但是是O(N^2)的解原创 2015-01-19 15:00:32 · 1797 阅读 · 0 评论 -
LeetCode Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2 ↘ c原创 2015-01-05 07:33:08 · 2983 阅读 · 0 评论 -
LeetCode 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.思路分析:这题要求拷贝链表,包括内容,next指针和random指针。容易想原创 2015-04-20 14:08:09 · 1408 阅读 · 0 评论