最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

劍指Offer之Java算法習(xí)題精講鏈表專項訓(xùn)練

 更新時間:2022年03月22日 11:03:45   作者:明天一定.  
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會發(fā)現(xiàn)質(zhì)的變化

題目一

鏈表題——鏈表合并

根據(jù)給定的兩個升序鏈表合并為一個新的升序鏈表

具體題目如下

解法

/**
 * 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é)點查找返回鏈表入環(huán)的第一個節(jié)點

具體題目如下

?解法一

/**
 * 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é)點

根據(jù)給定的兩個鏈表頭節(jié)點按照指定條件查找起始節(jié)點

具體題目如下

解法一

/**
 * 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é)點并返回頭節(jié)點

具體題目如下

?解法

/**
 * 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é)點

根據(jù)給定的鏈表頭節(jié)點查找其中間節(jié)點

具體題目如下

?解法

/**
 * 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í)題精講鏈表專項訓(xùn)練的文章就介紹到這了,更多相關(guān)Java 鏈表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 淺析java中Pair和Map的區(qū)別

    淺析java中Pair和Map的區(qū)別

    這篇文章主要介紹了java中Pair和Map的區(qū)別,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • Spring使用AOP完成統(tǒng)一結(jié)果封裝實例demo

    Spring使用AOP完成統(tǒng)一結(jié)果封裝實例demo

    這篇文章主要介紹了Spring使用AOP完成統(tǒng)一結(jié)果封裝,本文通過實現(xiàn)demo給大家詳細(xì)講解,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-02-02
  • Java實現(xiàn)文件夾中內(nèi)容定時刪除

    Java實現(xiàn)文件夾中內(nèi)容定時刪除

    這篇文章主要為大家詳細(xì)介紹了Java實現(xiàn)文件夾中內(nèi)容定時刪除,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Maven Assembly實戰(zhàn)教程

    Maven Assembly實戰(zhàn)教程

    MavenAssembly插件用于創(chuàng)建可分發(fā)包,如JAR、ZIP或TAR文件,通過配置pom.xml,可以生成包含所有依賴的JAR文件或自定義格式的歸檔文件,示例展示了如何使用默認(rèn)描述符和自定義描述符創(chuàng)建JAR包,以及在多模塊項目中使用Assembly插件
    2024-12-12
  • 將bean注入到Spring中的方式總結(jié)

    將bean注入到Spring中的方式總結(jié)

    在Java的Spring框架中,將bean注入到容器中是核心概念之一,這是實現(xiàn)依賴注入的基礎(chǔ),Spring提供了多種方式來將bean注入到容器中,本文給大家總結(jié)了將bean注入到Spring中的幾種方式,需要的朋友可以參考下
    2023-12-12
  • Spring Boot 集成MyBatis 教程詳解

    Spring Boot 集成MyBatis 教程詳解

    這篇文章主要介紹了Spring Boot 集成MyBatis 教程詳解,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-04-04
  • idea中文件被Mark as Plain Text后恢復(fù)方式

    idea中文件被Mark as Plain Text后恢復(fù)方式

    在IntelliJ IDEA中,如果錯誤地將文件標(biāo)記為純文本(Mark as Plain Text),可以通過在項目目錄中右鍵點擊文件并選擇“Mark as”來恢復(fù)原文件類型
    2024-11-11
  • SpringBoot集成MQTT示例詳解

    SpringBoot集成MQTT示例詳解

    這篇文章主要為大家介紹了SpringBoot集成MQTT示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-07-07
  • java實現(xiàn)隨機生成驗證碼圖片

    java實現(xiàn)隨機生成驗證碼圖片

    這篇文章主要為大家詳細(xì)介紹了java實現(xiàn)隨機生成驗證碼圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • RocketMQ producer同步發(fā)送單向發(fā)送源碼解析

    RocketMQ producer同步發(fā)送單向發(fā)送源碼解析

    這篇文章主要為大家介紹了RocketMQ producer同步發(fā)送單向發(fā)送源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03

最新評論

东阳市| 仁寿县| 忻州市| 盘山县| 三亚市| 中超| 石棉县| 科技| 乐亭县| 玛纳斯县| 扬中市| 随州市| 沙雅县| 穆棱市| 永新县| 仙游县| 阜宁县| 锦州市| 萝北县| 北辰区| 宽甸| 综艺| 什邡市| 衢州市| 安西县| 临沧市| 彭州市| 寿宁县| 固阳县| 榆中县| 安阳市| 盘山县| 井冈山市| 东宁县| 孟州市| 兴化市| 荔波县| 含山县| 张家港市| 叙永县| 西乌|