福州網(wǎng)站怎么做的免費(fèi)網(wǎng)站誰(shuí)有靠譜的
一、24. 兩兩交換鏈表中的節(jié)點(diǎn)
題目:24. 兩兩交換鏈表中的節(jié)點(diǎn) - 力扣(LeetCode)
視頻:幫你把鏈表細(xì)節(jié)學(xué)清楚! | LeetCode:24. 兩兩交換鏈表中的節(jié)點(diǎn)_嗶哩嗶哩_bilibili
講解:代碼隨想錄
dummy->1->2->3->
注意操作的順序:
① dummy->2
② 2->1
③ 1->3
class Solution {public ListNode swapPairs(ListNode head) {if(head == null || head.next == null) return head;ListNode dummy = new ListNode(-1);dummy.next = head; //1ListNode cur = dummy;ListNode slow, fast;while(cur.next != null && cur.next.next != null){ //3//在這里用cur同時(shí)定位slow和fast的相對(duì)位置 //2slow = cur.next;fast = slow.next.next;cur.next = slow.next;cur.next.next = slow;slow.next = fast;cur = slow;}return dummy.next;}
}
注意:
1、定義完虛擬頭結(jié)點(diǎn)之后,記得連在頭結(jié)點(diǎn)之前;
2、fast 和 slow 指針?lè)旁谘h(huán)中,用cur同時(shí)定位slow和fast的相對(duì)位置,省了每次定位 fs 兩個(gè)指針的代碼;
3、這里不能寫(xiě)成 ||,因?yàn)閷?xiě)成 || 節(jié)點(diǎn)是奇數(shù)個(gè)就無(wú)法判斷到后面的條件
只要 cur.next
或 cur.next.next
中有一個(gè)不為 null
,循環(huán)就會(huì)繼續(xù)。這意味著即使 cur.next
為 null
,只要 cur.next.next
不為 null
,循環(huán)仍然會(huì)繼續(xù),這會(huì)導(dǎo)致 NullPointerException
,因?yàn)槟阍噲D訪問(wèn) null
的 next
屬性。
嘗試過(guò)程:
class Solution {public ListNode swapPairs(ListNode head) {if(head == null || head.next == null) return head;ListNode dummy = new ListNode(-1);dummy.next = head; //ListNode cur = dummy;ListNode slow = head;ListNode fast = head.next.next;while(cur.next != null || slow.next != null){cur.next = slow.next;cur.next.next = slow;slow.next = fast;cur = slow;slow = fast;fast = slow.next.next; //這里有問(wèn)題}return dummy.next;}
}
在處理鏈表成對(duì)交換時(shí)存在一些邏輯問(wèn)題,特別是在更新fast
指針和處理鏈表末尾的部分,報(bào)了空指針異常。
解決辦法是:把 fast 和 slow 指針?lè)旁谘h(huán)里改變
二、19.刪除鏈表的倒數(shù)第N個(gè)節(jié)點(diǎn)
題目:19. 刪除鏈表的倒數(shù)第 N 個(gè)結(jié)點(diǎn) - 力扣(LeetCode)
視頻:鏈表遍歷學(xué)清楚! | LeetCode:19.刪除鏈表倒數(shù)第N個(gè)節(jié)點(diǎn)_嗶哩嗶哩_bilibili
講解:代碼隨想錄
雙指針的經(jīng)典應(yīng)用
思路: 如果要?jiǎng)h除倒數(shù)第n個(gè)節(jié)點(diǎn),讓fast移動(dòng)n步,然后讓fast和slow同時(shí)移動(dòng),直到fast指向鏈表末尾。刪掉slow所指向的節(jié)點(diǎn)就可以了。
class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {if(head == null) return null;ListNode dummy = new ListNode(-1, head); //1ListNode fast = dummy, slow = dummy;for(int i=0; i<=n; i++){ //2fast = fast.next;}while(fast!=null){ //3fast = fast.next;slow = slow.next;}slow.next = slow.next.next;return dummy.next; }
}
注意:
1、接在 head 之前用這一步寫(xiě)就行: 初始化一個(gè)空結(jié)點(diǎn),初始賦值為0,并且list的下一個(gè)next指針指向head,指針指向?yàn)閘ist: ListNode list=new ListNode(0,head);
2、注意終止條件,如果是 i<=n,加上=,slow 指針到時(shí)可以剛好停在刪除元素的前一個(gè)節(jié)點(diǎn)
3、終止條件是 fast 判空,不是 fast.next 判空
嘗試過(guò)程:
class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {if(head == null) return -1; //1ListNode dummy = new ListNode(-1, head);ListNode fast = dummy, slow = dummy;for(int i=0; i<=n; i++){fast = fast.next;}while(fast.next!=null){ //2fast = fast.next;slow = slow.next;}slow.next = slow.next.next;return dummy.next;}
}
1、返回值類(lèi)型錯(cuò)誤:如果鏈表為空,應(yīng)該返回null
而不是-1
,因?yàn)?code>-1不是一個(gè)有效的鏈表節(jié)點(diǎn)。
2、見(jiàn)上面
三、面試題 02.07. 鏈表相交
同:160.鏈表相交
題目:面試題 02.07. 鏈表相交 - 力扣(LeetCode)
視頻:
講解:代碼隨想錄
四、142.環(huán)形鏈表II
題目:142. 環(huán)形鏈表 II - 力扣(LeetCode)
視頻:把環(huán)形鏈表講清楚! 如何判斷環(huán)形鏈表?如何找到環(huán)形鏈表的入口? LeetCode:142.環(huán)形鏈表II_嗶哩嗶哩_bilibili
講解:代碼隨想錄