<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)141: 環(huán)形鏈表

          共 1544字,需瀏覽 4分鐘

           ·

          2021-01-04 20:32

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?環(huán)形鏈表,我們先來看題面:
          https://leetcode-cn.com/problems/linked-list-cycle/

          Given head, the head of a linked list, determine if the linked list has a cycle in it.


          There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail's next pointer is connected to. Note that pos is not passed as a parameter.


          Return true if there is a cycle in the linked list. Otherwise, return false.

          題意


          給定一個鏈表,判斷鏈表中是否有環(huán)。
          如果鏈表中有某個節(jié)點,可以通過連續(xù)跟蹤 next 指針再次到達(dá),則鏈表中存在環(huán)。為了表示給定鏈表中的環(huán),我們使用整數(shù) pos 來表示鏈表尾連接到鏈表中的位置(索引從 0 開始)。如果 pos 是 -1,則在該鏈表中沒有環(huán)。注意:pos 不作為參數(shù)進(jìn)行傳遞,僅僅是為了標(biāo)識鏈表的實際情況。
          如果鏈表中存在環(huán),則返回 true 。否則,返回 false 。
          進(jìn)階:你能用 O(1)(即,常量)內(nèi)存解決此問題嗎?


          樣例


          解題


          用一個hashSet來存儲已經(jīng)遍歷過的節(jié)點
          一旦發(fā)現(xiàn)某個節(jié)點的next節(jié)點已經(jīng)被遍歷過,則說明存在環(huán)。
          時間復(fù)雜度和空間復(fù)雜度均是O(n),其中n為鏈表中的節(jié)點個數(shù)。

          public?class?Solution1?{
          ????public?boolean hasCycle(ListNode head) {
          ????????HashSet hashSet = new?HashSet<>();
          ????????ListNode dummyHead = new?ListNode(-1);
          ????????dummyHead.next = head;
          ????????ListNode cur = dummyHead;
          ????????while(null?!= cur.next){
          ????????????if(hashSet.contains(cur.next)){
          ????????????????return?true;
          ????????????}
          ????????????cur = cur.next;
          ????????????hashSet.add(cur);
          ????????}
          ????????return?false;
          ????}
          }



          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力。

          上期推文:

          LeetCode1-140題匯總,希望對你有點幫助!


          瀏覽 21
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  多人操逼 | 成人三级视频在线观看 | 久久国产高清视频免费看 | 欧美三级全黄做爰大尺 | 九九爱精品视频 |