链表面试题 进阶 二

来源:互联网 发布:淘宝金币大转盘 编辑:程序博客网 时间:2024/06/07 22:22

一:1寻找两条链表里面的相同元素

       2寻找两条连表里面不同的元素  (都不考虑带环问题)

二:复杂链表的复制。一个链表的每个节点,有一个指向next指针指向下一个节点,还有一个random指针指向这个链表中的一个随机节点或者NULL,现在要求实现复制这个链表,返回复制后的新链表。 


//ps: 复杂链表的结构
struct ComplexNode
{
int _data ; // 数据
struct ComplexNode * _next; // 指向下一个节点的指针
struct ComplexNode * _random; // 指向随机节点(可以是链表中的任意节点 or 空)
}; 


1 寻找两条链表里面的相同元素

   不考虑n次循环求相同元素,要求只能每条链表便利一次。



链表的冒泡排序在另一篇博客 http://blog.csdn.net/zhang1308299607/article/details/73071824

排序完成后

void Find_The_Same(ListNode* pList1,ListNode* pList2){//1含有空链表//2不含空链表  ListNode* curList1 = pList1;  ListNode* curList2 = pList2;  if(pList1 == NULL || pList2 == NULL)  {        return ;  }  while(curList1 && curList2)  {if(curList1->data == curList2->data){printf("%d ",curList1->data);curList1 = curList1->next;curList2 = curList2->next;}else if(curList1->data > curList2->data){curList2 = curList2->next;}else{curList1 = curList1->next;}  }}

  2寻找两条连表里面不同的元素 


void Find_The_Diffrent(ListNode* pList1,ListNode* pList2){//1含有空链表//2不含空链表  ListNode* curList1 = pList1;  ListNode* curList2 = pList2;  ListNode* tmp = NULL;  if(pList1 == NULL || pList2 == NULL)  {        return ;  }  while(curList1 && curList2)  {  if(curList1->data == curList2->data)  {  curList1 = curList1->next;  curList2 = curList2->next;  }  else if(curList1->data < curList2->data)  {printf("%d ",curList1->data);        curList1 = curList1->next;  }  else  {  printf("%d ",curList2->data);  curList2 = curList2->next;  }  }  if(curList1 != NULL)  tmp = curList1;  if(curList2 != NULL)      tmp = curList2;  while(tmp)  {    printf("%d ",tmp->data);    tmp = tmp->next;  }}


二 复杂链表复制

1原始链表:


2添加元素后的链表:


3给添加上去的节点random指针赋值:

每个新节点的random值就是自身前一个节点的random->next(前一个节点random不为空,如果为空那就是NULL) ,比如新节点1的random应该是NULL指向新节点3. 而 旧节点1的  random->next (恰指向是新节点3)      

4把新节点摘下来

注意恢复原始链表。                                                                                             

ComplexNode* CopyComplexList(ComplexNode* pList){//一:链表尾为空//1 返回//二:只有一个节点//1 复制//三 :有多个节点//1在每个节点后面插入一个相同的节点//2找出每个新节点的random值//3把每个节点摘下来然后链接起来ComplexNode* curpList = pList;if(pList == NULL){return NULL;}else if(pList->next == NULL){ComplexNode* Newhead = BuyComplexNode(pList->data);;Newhead->next = pList->next;Newhead->random = pList->random;    return Newhead;}else{ComplexNode* tmp;ComplexNode* head;ComplexNode* Next;ComplexNode* Newhead;while(curpList != NULL){//添加节点tmp = BuyComplexNode(curpList->data);tmp->next = curpList->next;curpList->next = tmp;curpList = curpList->next->next;}//添加random指针head = pList;Next = pList->next;while( Next->next){if(head->random != NULL)Next->random = head->random->next;elseNext->random = NULL;head = head->next->next;Next = Next->next->next;}if(head->random != NULL)Next->random = head->random->next;elseNext->random = NULL;//摘节点 恢复原链表head = pList;Next = pList->next;Newhead = Next;while(Next->next->next->next){head->next = head->next->next;Next->next = Next->next->next;head = head->next;Next = Next->next;}head->next = Next->next->next;Next->next = Next->next->next;return Newhead;}}





原创粉丝点击