劍指Offer之Java算法習(xí)題精講鏈表專項(xiàng)訓(xùn)練
題目一
鏈表題——鏈表合并
根據(jù)給定的兩個(gè)升序鏈表合并為一個(gè)新的升序鏈表
具體題目如下
解法
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { ListNode a = new ListNode(0),b = a; while(list1!=null&&list2!=null){ if(list1.val<=list2.val){ a.next = list1; list1 = list1.next; }else{ a.next = list2; list2 = list2.next; } a = a.next; } if(list1==null){ a.next = list2; } if(list2==null){ a.next = list1; } return b.next; } }
?題目二
鏈表題——查找鏈表
根據(jù)給定的鏈表頭文件判斷其中是否有環(huán)
具體題目如下
?解法一
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { HashSet<ListNode> set = new HashSet<ListNode>(); while(head!=null){ if(!set.add(head)){ return true; } set.add(head); head = head.next; } return false; } }
解法二
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public boolean hasCycle(ListNode head) { ListNode fast = head; ListNode slow = head; while(fast!=null){ if(fast.next==null) return false; slow = slow.next; fast = fast.next.next; if(fast==slow) return true; } return false; } }
題目三
鏈表題——查找數(shù)組中元素位置
根據(jù)給定的鏈表頭節(jié)點(diǎn)查找返回鏈表入環(huán)的第一個(gè)節(jié)點(diǎn)
具體題目如下
?解法一
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode detectCycle(ListNode head) { HashSet<ListNode> set = new HashSet<ListNode>(); while(head!=null){ if(!set.add(head)){ return head; } set.add(head); head = head.next; } return null; } }
解法二
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode detectCycle(ListNode head) { ListNode fast = head; ListNode slow = head; while(fast!=null){ if(fast.next==null) return null; slow = slow.next; fast = fast.next.next; if(slow == fast){ slow = head; break; } } while(fast!=null){ if(slow == fast){ return slow; } slow = slow.next; fast = fast.next; } return null; } }
題目四
鏈表題——查找鏈表相交起始節(jié)點(diǎn)
根據(jù)給定的兩個(gè)鏈表頭節(jié)點(diǎn)按照指定條件查找起始節(jié)點(diǎn)
具體題目如下
解法一
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { HashSet<ListNode> set = new HashSet<ListNode>(); while(headA!=null){ set.add(headA); headA = headA.next; } while(headB!=null){ if(!set.add(headB)){ return headB; } set.add(headB); headB = headB.next; } return null; } }
解法二
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { ListNode a = headA, b = headB; while(a != b){ if(a == null) a = headB; else a = a.next; if(b == null) b = headA; else b = b.next; } return a; } }
題目五
鏈表題——鏈表操作
根據(jù)給定的鏈表刪除指定節(jié)點(diǎn)并返回頭節(jié)點(diǎn)
具體題目如下
?解法
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode removeNthFromEnd(ListNode head, int n) { ListNode node = new ListNode(-1); node.next = head; ListNode x = findFromEnd(node,n+1); x.next = x.next.next; return node.next; } private ListNode findFromEnd(ListNode head, int k) { ListNode fast = head; ListNode slow = head; for(int i = 0;i<k;i++){ fast = fast.next; } while(fast!=null){ slow = slow.next; fast = fast.next; } return slow; } }
題目六
鏈表題——查找鏈表中間節(jié)點(diǎn)
根據(jù)給定的鏈表頭節(jié)點(diǎn)查找其中間節(jié)點(diǎn)
具體題目如下
?解法
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode middleNode(ListNode head) { ListNode fast = head ; ListNode slow = head ; while(fast!=null){ if(fast.next == null) return slow; slow = slow.next; fast = fast.next.next; } return slow; } }
到此這篇關(guān)于劍指Offer之Java算法習(xí)題精講鏈表專項(xiàng)訓(xùn)練的文章就介紹到這了,更多相關(guān)Java 鏈表內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 劍指Offer之Java算法習(xí)題精講鏈表與數(shù)組專項(xiàng)訓(xùn)練
- 劍指Offer之Java算法習(xí)題精講鏈表與二叉樹(shù)專項(xiàng)訓(xùn)練
- 劍指Offer之Java算法習(xí)題精講鏈表與字符串及數(shù)組
- Java?數(shù)據(jù)結(jié)構(gòu)與算法系列精講之單向鏈表
- Java 數(shù)據(jù)結(jié)構(gòu)與算法系列精講之環(huán)形鏈表
- Java實(shí)現(xiàn)順序表和鏈表結(jié)構(gòu)
- Java實(shí)現(xiàn)單鏈表基礎(chǔ)操作
- Java關(guān)于重排鏈表詳細(xì)解析
- Java?詳解分析鏈表的中間節(jié)點(diǎn)
相關(guān)文章
Spring Cloud Gateway + Nacos 實(shí)現(xiàn)動(dòng)態(tài)路由
這篇文章主要介紹了Spring Cloud Gateway + Nacos 實(shí)現(xiàn)動(dòng)態(tài)路由的方法,幫助大家實(shí)現(xiàn)路由信息的自動(dòng)更新,感興趣的朋友可以了解下2020-10-10深入理解 Java、Kotlin、Go 的線程和協(xié)程
這篇文章主要介紹了深入理解 Java、Kotlin、Go 的線程和協(xié)程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12解決Error:(1,?1)?java:?非法字符:?'\ufeff'問(wèn)題
這篇文章主要介紹了解決Error:(1,?1)?java:?非法字符:?'\ufeff'問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11AgileBoot?項(xiàng)目?jī)?nèi)統(tǒng)一的錯(cuò)誤碼設(shè)計(jì)分析
這篇文章主要為大家介紹了AgileBoot?項(xiàng)目?jī)?nèi)統(tǒng)一的錯(cuò)誤碼設(shè)計(jì)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10關(guān)于SpringCloud的微服務(wù)結(jié)構(gòu)及微服務(wù)遠(yuǎn)程調(diào)用
Spring Cloud 是一套完整的微服務(wù)解決方案,基于 Spring Boot 框架,準(zhǔn)確的說(shuō),它不是一個(gè)框架,而是一個(gè)大的容器,它將市面上較好的微服務(wù)框架集成進(jìn)來(lái),從而簡(jiǎn)化了開(kāi)發(fā)者的代碼量,需要的朋友可以參考下2023-05-05關(guān)于fastjson的常見(jiàn)API詳解
這篇文章主要介紹了關(guān)于fastjson的常見(jiàn)API詳解,Fastjson是一個(gè)Java庫(kù),可用于將Java對(duì)象轉(zhuǎn)換為其JSON表示,它還可用于將JSON字符串轉(zhuǎn)換為等效的Java對(duì)象,Fastjson可以處理任意Java對(duì)象,包括您沒(méi)有源代碼的預(yù)先存在的對(duì)象,需要的朋友可以參考下2023-07-07關(guān)于多線程常用方法以及對(duì)鎖的控制(詳解)
下面小編就為大家?guī)?lái)一篇關(guān)于多線程常用方法以及對(duì)鎖的控制(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05