链表面试题 进阶 二

一: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;
			else
				Next->random = NULL;
			head = head->next->next;
			Next = Next->next->next;
		}
		if(head->random != NULL)
				Next->random = head->random->next;
		else
				Next->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;
	}

}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值