摘要: 【链表】Q:Implement an algorithm to find the nth to last element of a singly linked list .题目:找出链表的倒数第n个节点元素。解答: 方法一:利用两个指针p,q,首先将q往链表尾部移动n位,然后再将p、q一起往后移,那么当q达到链表尾部时,p即指向链表的倒数第n个节点。node* find_nth_to_last(node* head,int n) { if(head==NULL || nnext; } if(n>=0) return NULL; while(p!=NULL && q!=NU 阅读全文
posted @ 2013-10-20 09:28 mkdir 阅读(223) 评论(0) 推荐(0) 编辑
摘要: 【链表】Q:Implement an algorithm to delete a node in the middle of a single linked list, given only access to that node EXAMPLEInput: the node ‘c’ from the linked list a->b->c->d->eResult: nothing is returned, but the new linked list looks like a->b->d->e题目:实现算法来删除单链表中的中间节点(只知道指向该节点 阅读全文
posted @ 2013-10-20 09:27 mkdir 阅读(250) 评论(0) 推荐(0) 编辑
摘要: 【链表】Q:Write code to remove duplicates from an unsorted linked list FOLLOW UP How would you solve this problem if a temporary buffer is not allowed?题目:编码实现从无序链表中移除重复项。 如果不能使用临时缓存,你怎么编码实现?解答: 方法一:不使用额外的存储空间,直接在原始链表上进行操作。首先用一个指针指向链表头节点开始,然后遍历其后面的节点,将与该指针所指节点数据相同的节点删除。然后将该指针后移一位,继续上述操作。直到该指针移到链表。void .. 阅读全文
posted @ 2013-10-20 08:58 mkdir 阅读(998) 评论(0) 推荐(1) 编辑
摘要: 创建链表、往链表中插入数据、删除数据等操作,以单链表为例。 1.使用C语言创建一个链表:typedef struct nd{ int data; struct nd* next; } node; //初始化得到一个链表头节点 node* init(void){ node* head=(node*)malloc(sizeof(node)); if(head==NULL) return NULL; head->next=NULL; return head;} //在链表尾部插入数据void insert(node* head,int data){ if(head==NU... 阅读全文
posted @ 2013-10-20 08:57 mkdir 阅读(203) 评论(0) 推荐(0) 编辑