链表面试题

来源:互联网 发布:节目点播安庆网络广播 编辑:程序博客网 时间:2024/05/29 06:46
从尾到头打印单链表(栈  递归)
//从尾到头打印链表#include<stack>void PrintTailToHead(ListNode* Head){stack<ListNode*> s;ListNode* Node=Head;while(Node!=NULL){s.push(Node);Node=Node->_next;}while(!s.empty()){cout<<s.top()->_data<<" ";s.pop();}}   //空间复杂度O(n)void PrintTailToHeadR(ListNode* Head){if(Head!=NULL){if(Head->_next!=NULL){PrintTailToHead(Head->_next);}cout<<Head->_data<<" ";}}

.删除一个无头单链表的非尾结点

//删除一个无头单链表的非尾结点#include<assert.h>void deletenode(ListNode* pos){assert(pos);if(pos->_next==NULL)return;ListNode* next=pos->_next;ListNode* nextnext=pos->_next->_next;pos->_data=next->_data;pos->_next=nextnext;delete next;}
在无头单链表的一个非头结点前插入一个结点
void PushNoHead(ListNode* Head,ListNode* pos,int x){assert(pos);if(pos==Head)return;ListNode* prev=Head;while(prev->_next!=pos){prev=prev->_next;}ListNode* Node=new ListNode();Node->data=x;Node->_next=pos;prev->_next=Node;}
反转单链表
ListNode* ReverseList(ListNode* Head){ListNode* ReverseHead=NULL;ListNode* Node=Head;ListNode* prev=NULL;while(Node!=NULL){ListNode* Next=Node->_next;if(Next==NULL)ReverseHead=Node;Node->_next=prev;prev=Node;Node=Next;}return ReverseHead;}ListNode* ReserveList(ListNode* Head){if(Head==NULL)return ;ListNode* ReserveHead=NULL;ListNode* cur=Head;while(cur!=NULL){ListNode* tmp=cur;cur=cur->_next;tmp->_next=ReserveHead;tmp=ReserveHead;}return ReserveHead;}
单链表排序
void BubbleSort(ListNode* Head){if(Head==NULL||Head->_next==NULL)return;ListNode* tail=NULL;while(tail!=Head){int flag=0;ListNode* cur=Head;    ListNode* next=cur->_next;while(next!=tail){if(cur->data>next->data){   swap(cur->data,next->data);   flag=1;}cur=cur->_next;next=next->_next;}if(flag==0)break;tail=cur;}}//前后指针法   cur prev   prev指向cur的前一个 cur向后走找比key小的值,找到之后prev++  二者不等 交换 找不到一直走 cur走到右边界 prev++   交换二者void QuickSort(ListNode* Head,ListNode* tail){if(Head==NULL||Head->_next==NULL)return;ListNode* prev=Head;ListNode* cur=prev->_next;int key=Head->data;while(cur!=tail){if(cur->data<key){prev=prev->_next;if(cur!=prev)swap(cur->data,prev->data);}cur=cur->_next;}swap(prev->data,Head->data);QuickSort(Head,prev);QuickSort(prev->_next,tail);}

合并两个有序链表      升序降序都可以处理:1.用仿函数 2.函数指针 (传参传函数指针)
ListNode* MergeList(ListNode* Head1,ListNode* Head2){if(Head1==NULL)return Head2;if(Head2==NULL)return Head1;ListNode* MergeHead=NULL;if(Head1->_data<Head2->_data){MergeHead=Head1;MergeHead->_next=MergeList(Head1->_next,Head2);}else{MergeHead=Head2;MergeHead->_next=MergeList(Head1,Head2->_next);}return MergeHead;}ListNode* MergeListR(ListNode* Head1,ListNode* Head2){if(Head1==NULL)return Head2;if(Head2==NULL)return Head1;ListNode* MergeHead=NULL;ListNode* Node1=Head1;ListNode* Node2=Head2;if(Node1->_data<Node2->_data)//合并后的头结点{MergeHead=Node1;MergeHead->_next=NULL;Node1=Node1->_next;}else{MergeHead=Node2;MergeHead->_next=NULL;Node2=Node2->_next;}ListNode* tmp=MergeHead;while(Node1!=NULL&&Node2!=NULL){if(Node1->_data<Node2->_data){tmp->_next=Node1;    Node1=Node1->_next;    tmp=tmp->_next;    tmp->_next=NULL;}else{tmp->_next=Node2;Node2=Node2->_next;tmp=tmp->_next;tmp->_next=NULL;}}if(Node1==NULL)tmp->_next=Node2;if(Node2==NULL)tmp->_next=Node1;return MergeHead;}
判断单链表是否带环,若带环,求环的长度。求环的入口点
//判断链表带环,带环返回相遇点,不带环,返回NULLListNode* CirList(ListNode* Head){ListNode* fast=Head;ListNode* slow=Head;while(fast&&fast->_next){slow=slow->_next;fast=fast->_next->_next;if(slow==fast)return slow;}return NULL;}//若链表带环,求环的长度   从相遇点开始走,走到相遇点int CirLength(ListNode* Head){ListNode* meet=CirList(Head);ListNode* cur=meet;int count=1;while(cur->_next!=meet){count++;cur=cur->_next;}return count;}//若带环,求入口点 一个指针从相遇点开始走,一个指针从头开始走,两个指针相遇,相遇点为入口点ListNode* EntryNode(ListNode* Head){ListNode* Node1=Head;ListNode* Node2=CirList(Head);while(Node1!=Node2){Node1=Node1->_next;Node2=Node2->_next;}return Node1;}//判断两个链表是否相交(不带环)bool Issame(ListNode* Head1,ListNode* Head2){ListNode* Node1=Head1;ListNode* Node2=Head2;while(Node1->_next!=NULL){Node1=Node1->_next;}while(Node2->_next!=NULL){Node2=Node2->_next;}if(Node1==Node2)return true;elsereturn false;}//若相交,求交点ListNode* meetNode(ListNode* Head1,ListNode* Head2){if(!Issame(Head1,Head2))return NULL;ListNode* Node1=Head1;ListNode* Node2=Head2;int Length1=0;int Length2=0;while(Node1!=NULL){Length1++;Node1=Node1->_next;}while(Node2!=NULL){Length2++;Node2=Node2->_next;}ListNode* shortnode=NULL;ListNode* longnode=NULL;int div=0;if(Length1>Length2){longnode=Head1;shortnode=Head2;div=Length1-Length2;}else{longnode=Head2;shortnode=Head1;div=Length2-Length1;}for(int i=0;i<div;i++){longnode=longnode->_next;}while(shortnode!=longnode){shortnode=shortnode->_next;longnode=longnode->_next;}return shortnode;}//链表相交带环//一个相遇点动,另一个相遇点不动,看两个是否相遇,相遇则带环链表相交,不相遇,带环链表不相交//环内相交,环外相交   求入口点,看入口点是不是相同,相同,环外相交,不相同,环内相交
查找单链表的倒数第k个结点。
ListNode* FindKToTail(ListNode* Head,unsigned int k){if(Head==NULL||k==0)return NULL;ListNode* slow=Head;ListNode* fast=Head;for(int i=0;i<k-1;++i){if(fast->_next!=NULL){fast=fast->_next;}else{    return NULL;}}while(fast->_next!=NULL){fast=fast->_next;slow=slow->_next;}return slow;}
复杂链表的复制
void CloneNode(ComplexNode* Head){ComplexNode* cur=Head;while(cur!=NULL){ComplexNode* Node=new ComplexNode(cur->_data);Node->_next=cur->_next;Node->_random=NULL;cur->_next=Node;cur=Node->_next;}}void Clonerandom(ComplexNode* Head){ComplexNode* cur=Head;while(cur!=NULL){if(cur->_random!=NULL){cur->_next->_random=cur->_random->_next;}cur=cur->_next->_next;}}ComplexNode* CloneComplex(ComplexNode* Head){ComplexNode* CloneHead=NULL;ComplexNode* CloneNode=NULL;ComplexNode* cur=Head;if(cur!=NULL){CloneHead=CloneNode=cur->_next;cur->_next=cur->_next->_next;cur=cur->_next;}while(cur!=NULL){CloneNode->_next=cur->_next;CloneNode=CloneNode->_next;cur->_next=CloneNode->_next;cur=cur->_next;}return CloneHead;}void Clone(ComplexNode* Head){CloneNode(Head);Clonerandom(Head);CloneComplex(Head);}


原创粉丝点击