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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

《剑指offer》-- 两个链表的第一个公共结点、链表中环的入口结点、删除链表中的重复结点

發布時間:2024/9/30 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 《剑指offer》-- 两个链表的第一个公共结点、链表中环的入口结点、删除链表中的重复结点 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、兩個鏈表的第一個公共結點:

1、題目:

輸入兩個鏈表,找出它們的第一個公共結點。

2、解題思路:

(1)第一種:找出兩個鏈表的長度,然后讓長的鏈表先走兩個鏈表的長度差,接著兩個鏈表一起走。

(2)第二種:用兩個指針掃描"兩個鏈表",最終兩個指針到達 null 或者到達公共結點。接著,把鏈表1的尾連到鏈表2的頭,把鏈表2的尾連到鏈表1的頭,同時遍歷,最終會在公共結點相遇;

3、代碼實現:

public class Test9 {//解題方法二:找出兩個鏈表的長度,然后讓長的先走兩個鏈表的長度差,然后一起走public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {int length1 = listLength(pHead1);int length2 = listLength(pHead2);if(length1>length2){pHead1= walkStep(pHead1,length1-length2);}else{pHead2 = walkStep(pHead2,length2-length1);}while(pHead1 != null){if(pHead1 == pHead2) return pHead1;pHead1 = pHead1.next;pHead2 = pHead2.next;}return null;}//判斷兩個鏈表的的長度public int listLength(ListNode listNode){if(listNode == null) return 0;int length=0;while(listNode != null){listNode = listNode.next;length++;}return length;}//長度較長的鏈表先移動長度差public ListNode walkStep(ListNode listNode,int step){while(step>0){listNode = listNode.next;step--;}return listNode;}//解題思路一:用兩個指針掃描"兩個鏈表",最終兩個指針到達 null 或者到達公共結點。//把鏈表1的尾連到鏈表2的頭,把鏈表2的尾連到鏈表1的頭,同時遍歷,最終會在公共結點相遇;public ListNode FindFirstCommonNode1(ListNode pHead1, ListNode pHead2) {ListNode p1=pHead1;ListNode p2=pHead2;while(p1 != p2){p1=(p1==null ? pHead2 : p1.next);p2=(p2==null ? pHead1 : p2.next);}return p1;} }class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;} }

?

?

二、鏈表中環的入口結點:

1、題目:

給一個鏈表,若其中包含環,請找出該鏈表的環的入口結點,否則,輸出null。

2、解題思路:

參考牛客網的“求一個大大的offer”:https://www.nowcoder.com/questionTerminal/253d2c59ec3e4bc68da16833f79a38e4

假設x為環前面的路程(黑色路程),a為環入口到相遇點的路程(藍色路程,假設順時針走), c為環的長度(藍色+橙色路程)

當快慢指針相遇的時候:

此時慢指針走的路程為Sslow = x + m * c + a
快指針走的路程為Sfast = x + n * c + a
2 Sslow = Sfast
2 * ( x + m*c + a ) = (x + n *c + a)
從而可以推導出:
x = (n - 2 * m )*c - a
= (n - 2 *m -1 )*c + c - a
即環前面的路程 = 數個環的長度(為可能為0) + c - a
什么是c - a?這是相遇點后,環后面部分的路程。(橙色路程)
所以,我們可以讓一個指針從起點A開始走,讓一個指針從相遇點B開始繼續往后走,
2個指針速度一樣,那么,當從原點的指針走到環入口點的時候(此時剛好走了x)
從相遇點開始走的那個指針也一定剛好到達環入口點。
所以2者會相遇,且恰好相遇在環的入口點。

最后,判斷是否有環,且找環的算法復雜度為:

時間復雜度:O(n)? , 空間復雜度:O(1)

3、代碼實現:

public class Test13 {public ListNode EntryNodeOfLoop(ListNode pHead){if(pHead == null || pHead.next == null || pHead.next.next == null) return null;ListNode fast=pHead.next.next;ListNode slow=pHead.next;//先判斷有沒有環while(fast!=slow){if(fast.next !=null && fast.next.next !=null){fast=fast.next.next;slow=slow.next;}else{//沒有環,則返回return null;}}//循環出來就是有環,且此時fast==slowfast=pHead;while(fast != slow){fast = fast.next;slow = slow.next;}return slow;} }class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;} }

?

?

三、刪除鏈表中的重復結點:

1、題目:

在一個排序的鏈表中,存在重復的結點,請刪除該鏈表中重復的結點,重復的結點不保留,返回鏈表頭指針。 例如,鏈表1->2->3->3->4->4->5 處理后為 1->2->5

2、解決思路:

使用遞歸的方式:

3、代碼實現:

public class Test14 {public ListNode deleteDuplication(ListNode pHead){if(pHead == null ){return null;}if(pHead !=null && pHead.next==null){return pHead;}ListNode current;if(pHead.next.val == pHead.val){current = pHead.next.next;while(current!=null &&current.val == pHead.val){current = current.next;}return deleteDuplication(current);}else{current = pHead.next;pHead.next=deleteDuplication(current);return pHead;}} }class ListNode {int val;ListNode next = null;ListNode(int val) {this.val = val;} }

?

總結

以上是生活随笔為你收集整理的《剑指offer》-- 两个链表的第一个公共结点、链表中环的入口结点、删除链表中的重复结点的全部內容,希望文章能夠幫你解決所遇到的問題。

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