链式结构问题
ojshilu
https://github.com/lucky521
展开
-
2013.9.28微软笔试题-真题分析 链表大题 mircosoft
1.多进程,多线程。多进程下,每个进程都有自己的独立地址空间,进程间的数据空间也相互独立。多线程下,同一进程内的线程共享进程的地址空间,一个线程的数据可以直接提供给其他线程使用。原创 2013-10-01 18:32:58 · 1422 阅读 · 0 评论 -
删除有序链表的重复元素 Remove Duplicates from Sorted List
题目源自于leetcode。简单题目。题目:Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.思路:原创 2013-11-09 18:39:52 · 1146 阅读 · 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.思路:对于数组来说,右旋的方法是两轮逆转。对于链表,原创 2014-02-18 22:46:28 · 1047 阅读 · 0 评论 -
判断链表是否有环 Linked List Cycle
题目源自于leetcode。题目:Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:快指针、 慢指针。有环的话,一定不会遇到NULL;没环的话,fast一定先到NULL。代码:/** * Defin原创 2013-11-06 18:15:17 · 1763 阅读 · 0 评论 -
Reorder List
问题:Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…思路:采用的原来的简单做法,http://blog.csdn.net/ojshilu/article/details/12222035代码:/** * Definition for singly-linke原创 2014-02-28 20:23:11 · 827 阅读 · 0 评论 -
删除链表的倒数第n个结点 Remove Nth Node From End of List
题目源自于Leetcode。题目:Given a linked list, remove the nth node from the end of list and return its head.只允许过一遍。思路:特别主要当原链表节点个数小于n和等于n的时候这两种特殊情况。/** * Definition for singly-linked list.原创 2013-10-20 18:37:20 · 888 阅读 · 0 评论 -
合并k个有序链表 Merge k Sorted Lists
题目源自于leetcode。题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.解法:很直观的一个思路。每次在每个有序链表中寻找的表头寻找最小的一个,将其链到新链表上。复杂度:空间复杂度为O(1)。时间复杂度为O(k*n),k为有序链表的原创 2013-10-30 20:06:14 · 1539 阅读 · 0 评论 -
链表的二路归并排序 Sort List
对于链表,想找一种时间复杂度为O(nlogn),空间复杂度O(1)的算法。Sort a linked list in O(n log n) time using constant space complexity.首先想到了归并排序,因为链表的归并不需要辅助空间。思路:和数组的二路归并基本一致。所不同的是:1、要遍历一遍才能找到中间位置。2、首尾位置都要记下来,因为链表不是原创 2014-03-12 13:43:57 · 2893 阅读 · 1 评论 -
两个有序链表的合并 Merge Two Sorted Lists
1、记着怎么定义这样的结构体。typedef struct node{ int data; struct node *next;}node, *List;2、到哪儿都注意,防止指针为空、防止链表为空。List merge(List l1, List l2){ node *p3; List l3=NULL; if(l1==NULL) return l2原创 2013-09-05 12:55:33 · 1356 阅读 · 0 评论 -
把二叉树精简成单链表 Flatten Binary Tree to Linked List
题目:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2014-03-06 21:47:28 · 1043 阅读 · 0 评论 -
二叉树中增加next指针 Populating Next Right Pointers in Each Node
题目源自于leetcode。题目:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to原创 2013-11-06 20:58:02 · 1259 阅读 · 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 ea原创 2014-02-25 18:41:40 · 745 阅读 · 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.特殊输入:m=n,即逆转的部分只有一原创 2014-03-13 10:22:02 · 1163 阅读 · 0 评论 -
单链表和之恋 Bitwise add
题目和解答思路源自于待字闺中微信。题目:两个单链表,每一个节点里面一个0-9的数字, 输入就相当于两个大数了。然后返回这两个数的和(一个新list)。已知这两个输入的list 长度相等。 要求是:1. 不用递归。2. 要求算法在最好的情况下,只遍历两个list一次, 最差的情况下两遍。个人分析:如果这两个链表是按照数的低位在前、高位在后排的,那么作加法没有难度,进位发生在后面。原创 2013-09-18 19:20:01 · 1096 阅读 · 0 评论 -
交换单链表中相邻的两个元素 Swap Nodes in Pairs
题目源自于leetcode。题目:Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use onl原创 2013-10-30 20:39:59 · 3771 阅读 · 0 评论 -
链表的部分逆转 Reverse Nodes in k-Group
题目源自于leetcode。题目: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 sho原创 2013-11-05 20:47:33 · 1482 阅读 · 0 评论 -
求单链表环的入口结点 Linked List Cycle II
对于单链表,有的没有环,最后一个结点指向NULL,从头遍历到NULL就结束了。而有的是存在环,那么遍历时会在环内死循环,没有结尾。本题目源自于leetcode。经典题目。题目:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.solve it with原创 2013-11-22 21:21:56 · 2117 阅读 · 0 评论 -
逆置双向链表 Reverse a doubly linked list
逆转单链表,参见http://blog.csdn.net/ojshilu/article/details/9312239。本文是要逆转双向链表,其基本思想是一致的。只是要考虑的多一些。头尾结点都会出现NULL。双线链表结点的结构为 struct Node { int data; Node *next; Node *prev }这是个细心原创 2013-12-29 19:14:16 · 1940 阅读 · 0 评论 -
逆转链表 Reverse Linked List
#include#include#includeusing namespace std;typedef struct node{ int data; struct node *next;}node,*List;int main(){ List head = NULL; node *n; int input; cin>>input; while(input != -原创 2013-07-12 15:43:22 · 1354 阅读 · 0 评论 -
链表的插入排序 Insertion Sort List
使用插入排序算法对单链表进行排序。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {publi原创 2014-03-11 18:40:32 · 1207 阅读 · 0 评论 -
附有随机结点指针的链表的深度拷贝 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.思路:目前想到的办法是借助hash保存旧结点到原创 2014-03-13 18:27:36 · 1685 阅读 · 0 评论 -
Cache的设计和实现 LRU Cache
Cache的应用非常广泛,其实Cache是一种思想,一个广义的词汇,一种在性能和金钱的权衡上的思想。Cache的思想用在很多地方,使用的载体也不同,都是位于相对高速设备和相对低速设备之间,起到缓存的作用。1、最常用处:用在内存和CPU之间,以SRAM作为Cache的载体。(内存是DRAM,CPU是寄存器)2、用在内存和硬盘之间。3、用在硬盘和网络之间。原创 2014-03-31 09:52:26 · 2618 阅读 · 0 评论 -
两个链表的数字相加(简单题) add two numbers
题目: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 it as a原创 2013-10-03 12:32:48 · 1648 阅读 · 0 评论