![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
链表(linkedlist)
文章平均质量分 77
beiyetengqing
http://blog.csdn.net/beiyeqingteng 的镜像站
展开
-
merge two sorted linked list without duplicates
Question:Given two sorted linked list, and merge them without using extra space (using constant space is allowed). If there exist duplicated items, remove them and leave only one copy.public stati原创 2012-06-01 09:29:44 · 763 阅读 · 0 评论 -
Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]public class Solution原创 2012-12-30 12:47:42 · 778 阅读 · 0 评论 -
Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?public c原创 2012-12-30 13:02:28 · 1447 阅读 · 0 评论 -
Insert into a Cyclic Sorted List
Question:Given a node from a cyclic linked list which has been sorted, write a function to insert a value into the list such that it remains a cyclic sorted list. The given node can be any single no原创 2012-06-01 10:38:39 · 1003 阅读 · 0 评论 -
以k个元素为一组反转单向链表
问题:以k个元素为一组,反转单向链表。比如:输入: 1->2->3->4->5->6->7->8->null and k = 3输出:3->2->1->6->5->4->8->7->null. 分析:我们可以把整个链表分成多个长度为 k 的子链表, 然后,我们再反转每一个子链表(递归)。问题的关键是我们需要把每个子链表再连接起来。所以,对每一个子链表操作以后,我们需要返回原创 2012-05-24 06:12:32 · 7266 阅读 · 0 评论 -
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原创 2013-01-03 08:58:01 · 3217 阅读 · 0 评论 -
Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL./** * Definition for singly-link原创 2013-01-03 10:58:53 · 1223 阅读 · 0 评论 -
Copy A Linked List With Next And Random Pointer
Question: There is a doubly linked list with next pointer and random pointer (points to an arbitrary node in list). You have to make a copy of the linked list and return. In the end original list shou转载 2013-01-05 23:04:05 · 852 阅读 · 0 评论 -
eliminate the duplicates in a sorted linked list (1)
Qustion:Given a sorted linked list, and remove all the nodes which have duplication. For example, 1 -> 1 -> 2 -> 2 -> 3 ->4 , after removing duplicates, the linked list becomes 3 ->4.Analyze:1.原创 2012-05-27 08:12:46 · 609 阅读 · 0 评论 -
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原创 2012-12-30 12:11:08 · 1524 阅读 · 0 评论 -
eliminate the duplicates in the sorted list (2)
Question:Given a sorted list, and remove the duplicates in the list. For example, 1 -> 2 -> 2 -> 3, after removing the duplicates, we have 1 -> 2 -> 3public class Solution { public ListNod原创 2012-05-27 08:39:39 · 596 阅读 · 0 评论 -
Find the nth to last element of a singly linked list
Question:Implement an algorithm to find the nth to last (倒数第n个) element of a singly linked list.Analyze:we can simply use two pointers, and fix one position of the pointer and set it to the head原创 2012-06-01 23:17:02 · 1170 阅读 · 0 评论 -
add two linked list as integer
Question:You have two numbers represented by a linked list, where each node contains a single digit. Write a function that adds the two numbers and returns the sum as a linked list.EXAMPLE:input原创 2012-06-02 01:10:53 · 589 阅读 · 0 评论 -
使用递归和非递归方式反转单向链表
问题:给一个单向链表,把它从头到尾反转过来。比如: a -> b -> c ->d 反过来就是 d -> c -> b -> a 。分析:假设每一个node的结构是:class Node { char value; Node next;}因为在对链表进行反转的时候,需要更新每一个node的“next”值,但是,在更新 next 的值前,我们需要保存 next原创 2012-05-24 00:18:17 · 26616 阅读 · 3 评论 -
在O(1)平均时间删除链表结点
问题:给定链表的头指针和一个节点指针,在O(1)平均时间删除该节点。分析:我们可以从链表的头节点开始,顺序查找要删除的节点,找到之后再删除。即更新被删除节点的前一个节点的next值。由于需要顺序查找,时间复杂度自然就是O(n) 了。本文采用另一种方法:因为我们知道被删除节点的指针,所以,我们可以得到被删除节点的下一个节点,然后交换被删除节点和它下一个节点的值,然后让“被原创 2012-05-25 08:00:35 · 812 阅读 · 0 评论 -
判断两个链表是否相交
问题:给出两个单向链表的头指针,比如h1、h2,判断链表是否相交,如果不相交返回NULL;如果相交,返回指向第一个相交节点的指针。时间复杂度控制在O(n)。分析:如果两单向链表相交的话,一定是Y型相交,不可能出现X型,弄清楚这点后接下来的工作就是:(1)先找到h1,h2的最后一个节点L1和L2,同时记录节点数量a,b;(这里假设 a > b)(2)判断最后一个节点是否相同;原创 2012-05-25 23:49:11 · 1130 阅读 · 0 评论 -
判断一个单链表是否有环,如果有,找出环的起始位置
问题:判断一个单链表是否有环,如果有,找出环的起始位置。分析:我们可以从单链表head开始,每遍历一个,就把那个node放在hashset里,走到下一个的时候,把该node放在hashset里查找,如果有相同的,就表示有环,如果走到单链表最后一个node,在hashset里都没有重复的node,就表示没有环。 这种方法需要O(n)的空间和时间。另一种方法比较巧:设置两个指针原创 2012-05-26 09:41:15 · 3621 阅读 · 0 评论 -
逆序打印单向链表
问题:给你一个单向链表,比如 a -> b -> c -> d -> e -> f, 把这个链表逆序打印出来,即:f -> e -> d ->c ->b ->a思路:方法1:遍历链表,把每一个值按照顺序放在一个stack 里,当遍历完毕以后,再把 stack 里的每一个node取出来打印。方法2:利用递归的原理,先遍历到链表的最后一个节点,然后再打印。这样做本质上和方法1是一样的。原创 2012-05-26 11:36:01 · 2903 阅读 · 0 评论 -
merge two sorted linked list with duplicates
Question:Given two sorted linked list, and merge them without using extra space (using constant space is allowed). For example, if 1 -> 2 -> 5 merges with 2 -> 4 -> 5, we have 1 -> 2 -> 4 -> 5/*原创 2012-06-01 09:26:22 · 628 阅读 · 0 评论 -
O(N lgK) 时间内合并K个有序链表
问题:在O(N lgK) 时间内合并K个有序链表, 这里N指的是K个链表中所有的元素个数。分析:这是一道非常经典的面试题,在很多大公司的面试题中,此题频繁出现。这题也是算法导论的作业题。这题的思路如下:1) 在每一个链表中取出第一个值,然后把它们放在一个大小为K的数组里,然后把这个数组当成heap,然后把该堆建成最小堆。此步骤的时间复杂度为O(K)2 )取出堆中的最小值(也原创 2012-06-23 04:52:45 · 4301 阅读 · 0 评论