日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[算法]单链表专题

發布時間:2023/12/2 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [算法]单链表专题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如何判斷鏈表環的入口位置?

一個指針從頭開始單步走,一個指針從第一次相遇位置開始單步走,再相遇的位置就是環入口,證明如下:

設鏈表頭到環入口位置距離為a,入口位置到第一次相遇位置為b,相遇位置回到入口位置距離為c。

1、每次走一步的慢指針走了s步,則快指針走了2s步。s=a+b。相遇時慢指針一定沒走完一圈。

2、快指針走的總步數為a+b+n(b+c),走到相遇位置,并走了n圈。因此a+b+n(b+c)=2*(a+b)

a=n(b+c)-b=(n-1)(b+c)+c,也就證明了上面所提的方法。

?

class linklist { public:int val;linklist *next;linklist():next(NULL){}void push_back(int val);void clear();~linklist(){cout<<this->val<<" has been deleted"<<endl;} };void linklist::push_back(int val){linklist *p=this;while(p->next!=NULL){p=p->next;}p->next=new linklist;p->next->val=val; }void linklist::clear(){linklist *p=this,*ptmp;while(p!=NULL){ptmp=p->next;delete p;p=ptmp;} }linklist* merge(linklist *p1,linklist *p2){if(p1==NULL){return p2;}if(p2==NULL){return p1;}linklist *head,*cur;if(p1->val<=p2->val){head=p1;cur=p1;p1=p1->next;}else{head=p2;cur=p2;p2=p2->next;}while(p1!=NULL && p2!=NULL){if(p1->val<=p2->val){cur->next=p1;cur=p1;p1=p1->next;}else{cur->next=p2;cur=p2;p2=p2->next;}}if(p1!=NULL){cur->next=p1;}if(p2!=NULL){cur->next=p2;}return head; }linklist* merge_recur(linklist *p1,linklist *p2){if(!p1){return p2;}if(!p2){return p1;}if(p1->val<=p2->val){p1->next=merge_recur(p1->next,p2);return p1;}else{p2->next=merge_recur(p1,p2->next);return p2;} }linklist* reverse(linklist *head){linklist *pre,*cur,*next;pre=NULL;cur=head;while(cur){next=cur->next;//傳遞next的值放開頭,不放最后,可以避免cur為NULL的判斷cur->next=pre;pre=cur;cur=next;}return pre; }linklist* reverse_recur(linklist *head,linklist *&newhead){if(!head){return NULL;}linklist *p=reverse_recur(head->next,newhead);if(p){p->next=head;}else{newhead=head;}head->next=NULL;return head; }linklist* find_cross(linklist *p1,linklist *p2){if(!p1 || !p2){return NULL;}int len1=1,len2=1;linklist *ptmp=p1;while(ptmp->next){len1++;ptmp=ptmp->next;}linklist *p1end=ptmp;ptmp=p2;while(ptmp->next){len2++;ptmp=ptmp->next;}if(ptmp!=p1end){return NULL;}//兩個鏈表尾重疊了,證明鏈表交叉if(len1>len2){//去掉較長鏈表的多余部分,然后開始一一對比int count=len1-len2;while(count){p1=p1->next;count--;}}else if(len2>len1){int count=len2-len1;while(count){p2=p2->next;count--;}}while(true){if(p1==p2){return p1;}p1=p1->next;p2=p2->next;}return NULL; }linklist* find_loop(linklist *head){linklist *p1=head,*p2=head;do{p1=p1->next;if(p2->next){p2=p2->next->next;}else{return NULL;}}while(p1 && p2 && p1!=p2);if(!p1 || !p2){return NULL;}p2=head;while(p1!=p2){p1=p1->next;p2=p2->next;}return p1; }linklist* find_middle(linklist *head){linklist *fast=head,*slow=head;while(fast && fast->next){fast=fast->next->next;slow=slow->next;}return slow; }linklist* del_last_k(linklist *head,int k){/*linklist *reval;linklist *tmp=head,*pre_tmp=NULL;//用來存放當前節點的前k個節點的地址int i=k-1;linklist *p=head;while(p->next){if(i==0){//先判斷i,再進行i--,不能反過來pre_tmp=tmp;tmp=tmp->next;}p=p->next;if(i>0){i--;}}if(i>0){//如果i沒到0,證明k的大小比鏈表還長,不用進行刪除return head;}if(tmp==head){//如果刪除了鏈表頭,則需返回新的鏈表頭reval=head->next;}else{reval=head;}if(tmp){if(pre_tmp){pre_tmp->next=tmp->next;}delete tmp;tmp=NULL;}return reval;*/linklist *p=head,*reval;while(p && k>0){p=p->next;k--;}if(p==NULL && k==0){//剛好刪除第一個reval=head->next;delete head;return reval;}else if(p==NULL && k>0){//k長度超出鏈表長度,不需刪除return head;}else{//要刪的在第二個到最后一個之間,把p2移動到需要刪除的元素之前linklist *p2=head;while(p->next!=NULL){p=p->next;p2=p2->next;}linklist *tmp=p2->next;p2->next=p2->next->next;delete tmp;return head;} }//根據地址刪除一個中間節點:把刪除點后接節點的值復制到刪除點,然后刪除后接點 void DeleteMidNode(linklist *del){if (!del) {return;}linklist *next = del->next;if (next) {del->val= next->val;del->next= next->next;delete next;} }//在給定地址的節點前插入節點:在給定地址點后插入,然后交換前后兩節點的值 void insert_before(linklist *node,linklist *insert){if (!node || !insert) {return;}linklist temp = *node;node->val = insert->val;node->next= insert;*insert = temp; }

?

轉載于:https://www.cnblogs.com/iyjhabc/p/3254762.html

總結

以上是生活随笔為你收集整理的[算法]单链表专题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。