Posted 鍏ㄦ爤淇粰涔嬭矾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。

鍓嶈█

鏁版嵁缁撴瀯涓殑閾捐〃杩樻槸寰堥噸瑕佺殑锛屾墍浠ヨ繖绔犺妭鎶婂墤鎸噊ffer 鍜?LeetCode 涓殑鐩稿叧棰樼洰鍋氫竴涓眹鎬伙紝鍒嗕韩缁欏ぇ瀹娥煠€?/p>

閾捐〃 Linked List

涓€绉嶅父瑙佺殑鍩虹鏁版嵁缁撴瀯锛屼篃鏄竴绉嶇嚎鎬ц〃锛屼絾鏄苟涓嶄細鎸夌嚎鎬ц〃鐨勯『搴忓瓨鍌ㄦ暟鎹紝鑰屾槸鍦ㄦ瘡涓€涓妭鐐归噷瀛樺埌涓嬩竴涓妭鐐圭殑鎸囬拡(Pointer)銆?/p>

閾捐〃鍦ㄦ彃鍏ョ殑鏃跺€欏彲浠ヨ揪鍒?O(1) 鐨勫鏉傚害锛屾瘮鍙︿竴绉嶇嚎鎬ц〃 鈥斺€?椤哄簭琛ㄥ揩寰楀锛屼絾鏄煡鎵句竴涓妭鐐规垨鑰呰闂壒瀹氱紪鍙风殑鑺傜偣鍒欓渶瑕?O(n)鐨勬椂闂达紝鑰岄『搴忚〃鐩稿簲鐨勬椂闂村鏉傚害鍒嗗埆鏄?O(log n)  鍜?O(1)銆?/p>

浼樼己鐐癸細

浣跨敤閾捐〃缁撴瀯鍙互鍏嬫湇鏁扮粍閾捐〃闇€瑕侀鍏堢煡閬撴暟鎹ぇ灏忕殑缂虹偣锛岄摼琛ㄧ粨鏋勫彲浠ュ厖鍒嗗埄鐢ㄨ绠楁満鍐呭瓨绌洪棿锛屽疄鐜扮伒娲荤殑鍐呭瓨鍔ㄦ€佺鐞嗐€備絾鏄摼琛ㄥけ鍘讳簡鏁扮粍闅忔満璇诲彇鐨勪紭鐐癸紝鍚屾椂閾捐〃鐢变簬澧炲姞浜嗙粨鐐圭殑鎸囬拡鍩燂紝绌洪棿寮€閿€姣旇緝澶с€?/p>

閾捐〃鍏佽鎻掑叆鍜岀Щ闄よ〃涓婁换鎰忎綅缃笂鐨勮妭鐐癸紝浣嗘槸涓嶅厑璁搁殢鏈哄瓨鍙栥€?/strong>

閾捐〃鏈夊緢澶氱涓嶅悓鐨勭被鍨嬶細

  • 鍗曞悜閾捐〃
  • 鍙屽悜閾捐〃
  • 寰幆閾捐〃

閾捐〃閫氬父鍙互琛嶇敓鍑哄惊鐜摼琛紝闈欐€侀摼琛紝鍙岄摼琛ㄧ瓑銆傚浜庨摼琛ㄤ娇鐢紝闇€瑕佹敞鎰?span>澶寸粨鐐?/strong>鐨勪娇鐢ㄣ€?/p>

鍗曢摼琛?

閾捐〃鍒涘缓

//鍗曢摼琛ㄦ彃鍏ャ€佸垹闄ゃ€佹煡鎵?/span>
        class LinkedList {
            constructor(val) {
                val = val === undefined ? 'head' : val;
                this.head = new ListNode(val)
            }

            // 鎵緑al鍊艰妭鐐癸紝娌℃湁鎵惧埌杩斿洖-1
            findByVal(val) {
                let current = this.head
                while (current !== null && current.val !== val) {
                    current = current.next
                }
                return current ? current : -1
            }

            // 鎻掑叆鑺傜偣,鍦ㄥ€间负val鍚庨潰鎻掑叆
            insert(newVal, val) {
                let current = this.findByVal(val)
                if (current === -1return false
                let newNode = new ListNode(newVal)
                newNode.next = current.next
                current.next = newNode
            }

            // 鑾峰彇鍊间负nodeVal鐨勫墠涓€涓妭鐐?鎵句笉鍒颁负-1,鍙傛暟鏄痸al
            // 閫傜敤浜庨摼琛ㄤ腑鏃犻噸澶嶈妭鐐?/span>
            findNodePreByVal(nodeVal) {
                let current = this.head;
                while (current.next !== null && current.next.val !== nodeVal)
                    current = current.next
                return current !== null ? current : -1
            }

            // 鏍规嵁index鏌ユ壘褰撳墠鑺傜偣, 鍙傛暟涓篿ndex
            // 鍙互浣滀负姣旇緝閾捐〃鏄惁鏈夐噸澶嶈妭鐐?/span>

            findByIndex(index) {
                let current = this.head,
                    pos = 1
                while (current.next !== null && pos !== index) {
                    current = current.next
                    pos++
                }

                return (current && pos === index) ? current : -1
            }

            // 鍒犻櫎鏌愪竴涓妭鐐?鍒犻櫎澶辫触鏀惧洖false
            remove(nodeVal) {
                if(nodeVal === 'head'return false
                let needRemoveNode = this.findByVal(nodeVal)
                if (needRemoveNode === -1return false
                let preveNode = this.findNodePreByVal(nodeVal)
                
                preveNode.next = needRemoveNode.next
            }


            //閬嶅巻鑺傜偣

            disPlay() {
                let res = new Array()
                let current = this.head
                while (current !== null) {
                    res.push(current.val)
                    current = current.next
                }
                return res
            }

            // 鍦ㄩ摼琛ㄦ湯灏炬彃鍏ヤ竴涓柊鐨勮妭鐐?/span>
            push(nodeVal) {
                let current = this.head
                let node = new ListNode(nodeVal)
                while (current.next !== null)
                    current = current.next
                current.next = node
            }
            // 鍦ㄥご閮ㄦ彃鍏?/span>
            frontPush(nodeVal) {
                let newNode = new ListNode(nodeVal)
                this.insert(nodeVal,'head')
            }
        }

褰撶劧浜嗭紝鍙兘杩樻湁涓€浜涘叾浠栫殑鏂规硶鎴戞槸娌℃湁鎯冲埌鐨勶紝鍓╀笅鐨勫彲浠ヨ嚜琛屽幓瀹屾垚

閾捐〃绫荤殑浣跨敤

  let demo = new LinkedList() // LinkedList {head: ListNode}
        // console.log((demo.disPlay())) 
        demo.push('1232')
        demo.insert(123'head');
        demo.push('last value')
        demo.frontPush('start')
        demo.remove('head')
        // demo.remove('last value')
        // console.log(demo.remove('head'))
        // demo.push('2132')
        // demo.insert('涓嶅瓨鍦ㄧ殑鍊?, '鎻掑叆澶辫触') //return -1
        console.log(demo.findByIndex(1))
        console.log((demo.disPlay()))

涓婇潰鐨勪唬鐮佺墖娈垫槸娴嬭瘯鐢ㄥ埌锛屾祴璇曡繃浜嗭紝鍩烘湰涓婃病鏈変笂闈㈠ぇ闂锛屽綋鐒朵簡锛屾湁浜涚粏鏋濇湯鑺傜殑鍦版柟杩樻槸寰楁敞鎰忕殑锛屾瘮濡?code class="mq-122">findByIndex杩欎釜鍑芥暟涓?code class="mq-123">pos = 0 杩樻槸 pos = 1闂锛屽彇鍐充簬鑷繁锛岃繕鏈夌殑璇濓紝remove鍑芥暟鍒板簳鑳戒笉鑳藉垹闄?head'澶磋妭鐐癸紝杩欓兘鏄病鏈夊噯纭殑鏍囧噯鐨勶紝杩欎釜鍙互鏍规嵁鑷繁鎯呭喌鑰屽畾锛?/p>

涓€瀹氳浣忥紝涓嶆槸鍞竴鏍囧噯锛屼綘璁や负鍙互鍒犻櫎'head'鐨勮瘽锛屼篃娌℃湁闂銆?/p>

鍙屽悜閾捐〃

鍙岄摼琛ㄤ互绫讳技鐨勬柟寮忓伐浣滐紝浣?code class="mq-129">杩樻湁涓€涓紩鐢ㄥ瓧娈?/code>锛岀О涓?code class="mq-130">鈥減rev鈥?/code>瀛楁銆傛湁浜嗚繖涓澶栫殑瀛楁锛屾偍灏辫兘澶熺煡閬撳綋鍓嶇粨鐐圭殑鍓嶄竴涓粨鐐广€?/p>

璁╂垜浠湅涓€涓緥瀛愶細

銆岀畻娉曚笌鏁版嵁缁撴瀯銆嶉摼琛ㄧ殑9涓熀鏈搷浣?> 
  <figcaption class= 鍙岄摼琛?

缁胯壊绠ご琛ㄧず鎴戜滑鐨勨€減rev鈥濆瓧娈垫槸濡備綍宸ヤ綔鐨勩€?/p>

缁撴瀯绫讳技馃憞

class doubleLinkNode {
            constructor (val) {
                this.val = val
                this.prev = null
                this.next = null
            }
        }

涓庡崟閾炬帴鍒楄〃绫讳技锛屾垜浠皢浣跨敤澶寸粨鐐?/code>鏉ヨ〃绀烘暣涓垪琛ㄣ€?/p>

瀵逛簬鎻掑叆鍜屽垹闄わ紝鐩告瘮杈冨崟閾捐〃鑰岃█锛屼細绋嶅井澶嶆潅涓€浜涳紝鍥犱负鎴戜滑杩橀渶瑕佸鐞嗏€減rev鈥濆瓧娈点€?/p>

娣诲姞鎿嶄綔-鍙岄摼琛?/strong>

涓句釜渚嬪瓙鍚э紝褰撶劧浜嗭紝鏈€濂界殑褰㈠紡灏辨槸鐢诲浘鏉ヨВ鍐炽€?/p>

銆岀畻娉曚笌鏁版嵁缁撴瀯銆嶉摼琛ㄧ殑9涓熀鏈搷浣?> 
 </figure> 
 <p data-tool=璁╂垜浠湪鐜版湁缁撶偣 6 涔嬪悗娣诲姞涓€涓柊缁撶偣 9锛?/p>

绗竴姝ワ細閾炬帴 cur锛堢粨鐐?9锛変笌 prev锛堢粨鐐?6锛夊拰 next锛堢粨鐐?15锛?/p>

銆岀畻娉曚笌鏁版嵁缁撴瀯銆嶉摼琛ㄧ殑9涓熀鏈搷浣?> 
 </figure> 
 <p data-tool=绗簩姝ワ細鐢?cur锛堢粨鐐?9锛夐噸鏂伴摼鎺?prev锛堢粨鐐?6锛夊拰 next锛堢粨鐐?15锛?/p>
銆岀畻娉曚笌鏁版嵁缁撴瀯銆嶉摼琛ㄧ殑9涓熀鏈搷浣?> 
 </figure> 
 <p data-tool=鎵€浠ヨ锛屽仛閾捐〃棰橈紝鐢诲浘鏈€閲嶈浜嗭紝鐢诲畬鍥撅紝浠g爜涔熷氨鍑烘潵浜?/strong>

鐣欎笅鏉ヤ竴涓棶棰橈紝濡傛灉鎴戜滑鎯冲湪寮€澶?/code>鎴?code class="mq-171">缁撳熬鎻掑叆涓€涓柊缁撶偣鎬庝箞鍔烇紵

鍒犻櫎鎿嶄綔-鍙岄摼琛?/strong>

涓句釜渚嬪瓙鍚ю煈?/p>

鎴戜滑鐨勭洰鏍囨槸浠庡弻閾捐〃涓垹闄ょ粨鐐?6

鍥犳锛屾垜浠皢瀹冪殑鍓嶄竴涓粨鐐?23 鍜屼笅涓€涓粨鐐?15 閾炬帴璧锋潵锛?/p>

缁撶偣 6 鐜板湪涓嶅湪鎴戜滑鐨勫弻閾捐〃涓?/p>

鐣欎釜闂:濡傛灉鎴戜滑瑕佸垹闄?code class="mq-180">绗竴涓粨鐐?/code>鎴?code class="mq-181">鏈€鍚庝竴涓粨鐐?/code>鎬庝箞鍔烇紵

鐢诲浘馃き

浠g爜灏变笉鍐欎簡锛岀綉涓婂緢澶氶兘鍙互浠g爜锛屽彲浠ョ湅鐪嬩汉瀹舵€庝箞鍐欑殑

灏忕粨

璁╂垜浠畝瑕佸洖椤句竴涓嬪崟閾捐〃鍜屽弻閾捐〃鐨勮〃鐜般€?/p>

瀹冧滑鍦ㄥ緢澶氭搷浣滀腑鏄浉浼肩殑

  • 瀹冧滑閮借兘澶? 鍦?O(1) 鏃堕棿鍐呭垹闄ょ涓€涓粨鐐?/code>銆?
  • 瀹冧滑閮借兘澶? 鍦?O(1) 鏃堕棿鍐呭湪缁欏畾缁撶偣涔嬪悗鎴栧垪琛ㄥ紑澶存坊鍔犱竴涓柊缁撶偣銆?
  • 瀹冧滑閮芥棤娉曞湪甯搁噺鏃堕棿鍐? 闅忔満璁块棶鏁版嵁銆?

浣嗘槸鍒犻櫎缁欏畾缁撶偣(鍖呮嫭鏈€鍚庝竴涓粨鐐?鏃剁暐鏈変笉鍚屻€?/p>

  • 鍦ㄥ崟閾捐〃涓紝瀹冩棤娉曡幏鍙栫粰瀹氱粨鐐圭殑鍓嶄竴涓粨鐐癸紝鍥犳鍦ㄥ垹闄ょ粰瀹氱粨鐐逛箣鍓嶆垜浠繀椤昏姳璐? O(N) 鏃堕棿鏉ユ壘鍑哄墠涓€缁撶偣銆?
  • 鍦ㄥ弻閾捐〃涓紝杩欎細鏇村鏄擄紝鍥犱负鎴戜滑鍙互浣跨敤鈥減rev鈥濆紩鐢ㄥ瓧娈佃幏鍙栧墠涓€涓粨鐐广€傚洜姝ゆ垜浠彲浠ュ湪 O(1) 鏃堕棿鍐呭垹闄ょ粰瀹氱粨鐐广€?

瀵规瘮涓€涓嬮摼琛ㄤ笌鍏朵粬鏁版嵁缁撴瀯(鏁扮粍锛岄槦鍒楋紝鏍?涔嬮棿鏃堕棿澶嶆潅搴?/code>鐨勬瘮杈冿細

缁忚繃杩欐姣旇緝锛屾垜浠笉闅惧緱鍑虹粨璁猴細

濡傛灉浣犻渶瑕佺粡甯告坊鍔犳垨鍒犻櫎缁撶偣锛岄摼琛ㄥ彲鑳芥槸涓€涓笉閿欑殑閫夋嫨銆?/p>

濡傛灉浣犻渶瑕佺粡甯告寜绱㈠紩璁块棶鍏冪礌锛屾暟缁勫彲鑳芥槸姣旈摼琛ㄦ洿濂界殑閫夋嫨銆?/p>

鎺ヤ笅鏉ヤ篃灏辨槸鏈枃鐨勯噸鐐癸紝浠庣悊璁哄埌瀹為檯鍑哄彂锛岀湅鐪嬫湁鍝簺棰樺瀷鍚ю煈?/p>

鍩烘湰棰樺瀷

鎺ヤ笅鏉ョ殑棰樺瀷姊崇悊鏄寜鐓т釜浜哄埛棰橀『搴忕殑锛岄毦鏄撶▼搴︼紝涔熶細鍋氫釜鍒掑垎锛屽彲浠ュ弬鑰冧竴涓嬨€?/p>

涓昏鍋氶缃戠珯馃憞

  • 鍓戞寚offer

  • 鍔涙墸leetcode

鍚堝苟涓や釜鏈夊簭閾捐〃猸?span class="mq-220">

棰樼洰鎻忚堪锛氬皢涓や釜鍗囧簭閾捐〃鍚堝苟涓轰竴涓柊鐨?鍗囧簭 閾捐〃骞惰繑鍥炪€傛柊閾捐〃鏄€氳繃鎷兼帴缁欏畾鐨勪袱涓摼琛ㄧ殑鎵€鏈夎妭鐐圭粍鎴愮殑銆?/p>

閾炬帴锛歔鍔涙墸]鍚堝苟涓や釜鏈夊簭閾捐〃

绀轰緥锛?/strong>

杈撳叆锛?->2->4, 1->3->4
杈撳嚭锛?->1->2->3->4->4

闈為€掑綊鎬濊矾:

  • 妯℃嫙棰?閾捐〃

  • 鎬濊矾褰撶劧绠€鍗曪紝閲嶈鐨勬槸妯℃嫙杩囩▼锛屽湪绠楁硶绋嬪害涓婏紝杩欑棰樼洰鍙互杈冧负妯℃嫙棰橈紝妯℃嫙浣犳€濊€冪殑杩囩▼锛屾瘡娆℃瘮杈冧袱涓猯1.val 涓巐2.val鐨勫ぇ灏忥紝鍙栧皬鐨勫€硷紝鍚屾椂鏇存柊灏忕殑鍊兼寚鍚戜笅涓€涓妭鐐?/p>

  • 涓昏娉ㄦ剰鐨勫氨鏄惊鐜粓姝㈢殑鏉′欢锛氬綋涓よ€呭叾涓湁涓€涓负绌烘椂锛屽嵆鎸囧悜null

  • 鏈€鍚庨渶瑕佸垽鏂袱涓摼琛ㄥ摢涓潪绌猴紝鍦ㄥ皢闈炵┖鐨勯摼琛ㄤ笌tmp鍝ㄥ叺鑺傜偣杩炴帴灏卞ソ銆?/p>

  var mergeTwoLists = function (l1, l2{
            let newNode = new ListNode('start'),  // 鍋氶濂楄矾,澶磋妭鐐?/span>
                tmp = newNode;    // tmp浣滀负鍝ㄥ叺鑺傜偣    
            while (l1 && l2) {   // 寰幆缁撴潫鐨勬潯浠跺氨鏄袱鑰呴兘瑕佷负闈瀗ull
                if(l1.val >= l2.val) {
                    tmp.next = l2
                    l2 = l2.next
                }else{
                    tmp.next = l1
                    l1 = l1.next
                }
                tmp = tmp.next    // 鍝ㄥ叺鑺傜偣鏇存柊鎸囧悜涓嬩竴涓妭鐐?/span>
            }
            // 鏈€鍚庨渶瑕佸垽鏂摢涓摼琛ㄨ繕瀛樺湪闈瀗ull
            tmp.next = l1 == null ? l2 : l1;
            return newNode.next;
        };

閫掑綊鎬濊矾:閫掑綊瑙f硶瑕佹敞鎰忛€掑綊涓婚閲屾瘡娆¤繑鍥炲€艰緝灏忓緱鑺傜偣锛岃繖鏍锋墠鑳戒繚璇佹垜浠渶鍚庡緱鍒板緱鏄摼琛ㄥ緱鏈€灏忓紑澶?/strong>

涓€寮€濮嬬殑鍋氭硶灏辨槸妯℃嫙+閾捐〃锛屼絾鏄湅瑙佽璁哄尯涓湁閫掑綊鍐欐硶锛岀粷瀵硅繕鏄ソ濂界湅涓€閬嶃€備竴棰樺瑙h繕鏄緢閲嶈鐨勶紝杩欎篃鍦ㄦ煇绉嶇▼搴︿笂鍙戞暎浜嗘€濈淮锛岃繕鏄彁鍊″瑙c€?/p>

  • 閫掑綊鍑哄彛锛氫换鎰忎竴涓摼琛ㄤ负绌烘椂,鐩存帴return 鍙﹀涓€涓摼鎺ワ紝涔熷氨鏄嫾鎺ヨ繃绋?
  • 浠庝袱涓摼琛ㄤ腑渚濇鍙栧嚭鑺傜偣姣旇緝锛屽皬鐨勯偅涓€涓氨鎷庡嚭鏉ヤ綔涓轰笅涓€涓摼琛ㄨ妭鐐?

浠g爜鐐硅繖閲屸槕锔?/p>


杩斿洖鍊掓暟绗琸涓妭鐐光瓙

棰樼洰鎻忚堪锛氬疄鐜颁竴绉嶇畻娉曪紝鎵惧嚭鍗曞悜閾捐〃涓€掓暟绗?k 涓妭鐐广€傝繑鍥炶鑺傜偣鐨勫€笺€?/p>

閾炬帴锛歔鍔涙墸]杩斿洖鍊掓暟绗琸涓妭鐐?/p>

鍙屾寚閽堝啓娉曫煈?/p>

鎼炰咯涓墠鍚庢寚閽堬紝鍏堣鍚庢寚閽堣蛋k,鎺ョ潃涓や釜鎸囬拡灏辩浉宸甼姝?鏈€鍚庨亶鍘嗗悗鎸囬拡锛屽綋鍚庢寚閽堜负null鏃讹紝鍓嶆寚閽堝氨鏄瓟妗堬紝鍥犱负涓€寮€濮嬩粬浠袱灏辨槸鐩稿樊k璺濈

浠g爜鐐硅繖閲屸槕锔?/p>


鍙嶈浆閾捐〃猸?span class="mq-275">

棰樼洰鎻忚堪锛氬弽杞竴涓崟閾捐〃銆?/p>

閾炬帴锛歔leetcode]鍙嶈浆涓€涓摼琛?/p>

绀轰緥:

杈撳叆: 1->2->3->4->5->NULL
杈撳嚭: 5->4->3->2->1->NULL

鎬濊矾锛氳凯浠?涓変釜鎸囬拡 prev curr next 鍓嶆寚閽?褰撳墠鎸囬拡 涓嬩竴涓寚閽?/p>

  • 姣忔鎶婂綋鍓峜urr鎸囬拡鎸囧悜涓婁竴涓猵re
  • next淇濆瓨涓嬩竴涓妭鐐逛俊鎭?

灏忔妧宸э細涓€寮€濮嬫妸鍝ㄥ叺鑺傜偣璁剧疆涓簄ull锛宑urr璁剧疆涓篽ead

涓€鐩磋凯浠d笅鍙栵紝鐭ラ亾curr褰撳墠鑺傜偣涓哄熬鑺傜偣

        var reverseList = function (head{
            if(!head) return null
            let prev = null,
                curr = head
            while( curr != null) {
                let next = curr.next;
                curr.next = prev
                prev = curr
                curr = next
            }
            return prev
        };

閫掑綊鍐欐硶

涔嬪墠璁茶繃鎬濊矾浜嗭紝鎴戜滑涔嬮棿鐪嬩唬鐮佸惂

var reverseList = function(head{
    let reverse = (prev,curr) => {
        if(!curr)return prev;
        let next = curr.next;
        curr.next = prev;
        return reverse(curr,next);
    }
    return reverse(null,head);
};

浠g爜鐐硅繖閲屸槕锔?/p>


鍖洪棿鍙嶈浆猸愨瓙

棰樼洰鎻忚堪锛氬弽杞粠浣嶇疆 m 鍒?n 鐨勯摼琛ㄣ€傝浣跨敤涓€瓒熸壂鎻忓畬鎴愬弽杞€?/p>

璇存槑:1 鈮?m 鈮?n 鈮?閾捐〃闀垮害銆?/p>

閾炬帴锛歔leetcode]鍙嶈浆閾捐〃II

绀轰緥:

杈撳叆: 1->2->3->4->5->NULL, m = 2, n = 4
杈撳嚭: 1->4->3->2->5->NULL

璺熶笂涓€棰樺樊涓嶅锛屾崲姹や笉鎹㈣嵂锛屾墍浠ユ垜浠繕鏄彲浠ョ敤杩唬鐨勫仛娉曟潵瀹屾垚銆?/p>

闇€瑕佽褰曚袱涓妭鐐?tail鍜宖ront鑺傜偣

銆岀畻娉曚笌鏁版嵁缁撴瀯銆嶉摼琛ㄧ殑9涓熀鏈搷浣?> 
 </figure> 
 <p data-tool=涓や釜鑺傜偣浣滅敤灏辨槸涓轰簡鏈€鍚庡尯闂村弽杞悗锛屽ソ閲嶆柊杩炴帴鎴愪竴涓柊鐨勯摼琛ㄣ€?/p>
var reverseBetween = function (head, m, n{
            let count = n-m,
                newNode = new ListNode('head');
            tmp = newNode;
            tmp.next = head;              // 鍝ㄥ叺鑺傜偣,杩欐牱瀛愬悓鏃朵篃淇濊瘉浜唍ewNode涓嬩竴涓妭鐐瑰氨鏄痟ead
            for(let i = 0; i < m -1; i++ ){
                tmp = tmp.next;
            }
            // 姝ゆ椂寰幆鍚?tmp淇濈暀鐨勫氨鏄弽杞尯闂村墠涓€涓妭鐐?闇€瑕佺敤front淇濈暀涓嬫潵
            let front, prev, curr,tail;
            front = tmp;   // 淇濈暀鐨勬槸鍖洪棿棣栬妭鐐?/span>
            // 鍚屾椂tail鎸囬拡鐨勪綔鐢ㄦ槸灏嗗弽杞悗鐨勯摼鎺ュ埌鏈€鍚庤妭鐐?/span>

            prev = tail = tmp.next;    // 淇濈暀鍙嶈浆鍚庣殑闃熷熬鑺傜偣 涔熷氨鏄痶ail
            curr = prev.next
            for(let i = 0; i < count; i++ ) {
                let next = curr.next;
                curr.next = prev;
                prev = curr
                curr = next
            }
            // 灏嗗師鏈尯闂撮鑺傜偣閾炬帴鍒板悗缁撶偣
            tail.next = curr
            // font鏄尯闂村墠闈竴涓妭鐐?闇€瑕侀摼鎺ョ殑灏辨槸鍖洪棿鍙嶈浆鐨勬渶鍚庝竴涓妭鐐?/span>
            front.next = prev

            return newNode.next     // 鏈€鍚庤繑鍥瀗ewNode.next灏辫,涓€寮€濮嬫垜浠寚鍚戜簡head鑺傜偣

        };

鐐硅繖閲屼唬鐮侌煠?/p>


涓や袱浜ゆ崲閾捐〃涓殑鑺傜偣猸愨瓙

棰樼洰鎻忚堪锛氱粰瀹氫竴涓摼琛紝涓や袱浜ゆ崲鍏朵腑鐩搁偦鐨勮妭鐐癸紝骞惰繑鍥炰氦鎹㈠悗鐨勯摼琛ㄣ€?span>浣犱笉鑳藉彧鏄崟绾殑鏀瑰彉鑺傜偣鍐呴儴鐨勫€?/strong>锛岃€屾槸闇€瑕佸疄闄呯殑杩涜鑺傜偣浜ゆ崲銆?/p>

閾炬帴锛歭eetcode涓や袱浜ゆ崲閾捐〃涓殑鑺傜偣

绀轰緥:

缁欏畾 1->2->3->4, 浣犲簲璇ヨ繑鍥?nbsp;2->1->4->3.

杩唬鎬濊矾锛屽璺紝鍔犱釜tmp鍝ㄥ叺鑺傜偣灏辫鍝掞紝杩樹笉鎳傜殑璇濓紝鐢诲浘瑙e喅涓€鍒囷紝瀹炲湪鐪嬩笉鎳傜殑璇濓紝鐪嬭繖涓浘

 var swapPairs = function (head{
        let newNode = new ListNode('start');
            newNode.next = head,    // 閾捐〃澶磋妭鐐瑰璺搷浣?/span>
            tmp = newNode;          // tmp鍝ㄥ叺鑺傜偣,杩欓噷瑕佷粠newNode鑺傜偣寮€濮?骞朵笉鏄粠head寮€濮嬬殑 

        while( tmp.next !== null && tmp.next.next !== null) {
            let start = tmp.next,
                end = start.next;
            tmp.next = end
            start.next = end.next
            end.next = start
            tmp = start
        }

        return newNode.next     // 杩斿洖鐨勮嚜鐒跺氨鏄寚鍚?nbsp;閾捐〃澶磋妭鐐圭殑next鎸囬拡
    };

褰撶劧浜嗭紝闈㈣瘯鐨勬椂鍊欒鐪熺殑鍐欙紝鐢诲浘搴旇鍙互鐨勫惂锛岀湅鐫€鍥炬潵鍐欙紝灏辫交鏉句簡锛岃鐪熺殑锛屾垜閫掑綊鍐欐硶鉁嶆兂涓嶅嚭鏉ワ紝鎴戝ソ锠煠?/p>

浠g爜鐐硅繖閲屸槕锔?/p>


K 涓竴缁勭炕杞摼琛ㄢ瓙猸愨瓙

棰樼洰鎻忚堪锛氱粰浣犱竴涓摼琛紝姣?k 涓妭鐐逛竴缁勮繘琛岀炕杞紝璇蜂綘杩斿洖缈昏浆鍚庣殑閾捐〃銆?/p>

璇存槑锛歬 鏄竴涓鏁存暟锛屽畠鐨勫€煎皬浜庢垨绛変簬閾捐〃鐨勯暱搴︺€傚鏋滆妭鐐规€绘暟涓嶆槸 k 鐨勬暣鏁板€嶏紝閭d箞璇峰皢鏈€鍚庡墿浣欑殑鑺傜偣淇濇寔鍘熸湁椤哄簭銆?/p>

閾炬帴锛歔K 涓竴缁勭炕杞摼琛╙(https://leetcode-cn.com/problems/swap-nodes-in-pairs/)

绀轰緥 :

缁欏畾杩欎釜閾捐〃锛?->2->3->4->5
褰?nbsp;k = 2 鏃讹紝搴斿綋杩斿洖: 2->1->4->3->5
褰?nbsp;k = 3 鏃讹紝搴斿綋杩斿洖: 3->2->1->4->5

鍏堢湅棰樿В,leetcode猸愨瓙猸愰毦棰樼殑璇濓紝涓嶉渶瑕佸幓娴垂鏃堕棿鑷繁鍘绘€濊€冿紝鍙互鐪嬬湅鍒汉鐨勬€濊矾锛屾妸鍒汉鎬濊矾鎼炴槑鐧斤紝鏈€鍚庤浆鎹负鑷繁鐨勬€濊矾寰堥噸瑕併€傜湅瀹岀湡鐨勫氨椤挎偀浜嗭紝灏辩煡閬撹鎬庝箞瀹炵幇浜嗐€?/p>

  • 鍥犱负鏄痥涓垎缁勶紝鎵€浠ュ緱鏈変竴涓猚ount璁℃暟锛岃褰曡妭鐐逛釜鏁般€?
  • start鎸囬拡浠h〃鐨勫惈涔夊氨鏄? start璁板綍鐨勪俊鎭槸褰撳墠鍒嗙粍鐨勮捣濮嬭妭鐐逛綅缃殑鍓嶄竴涓妭鐐广€?
  • end鎸囬拡浠h〃鐨勫惈涔夊氨鏄鍖洪棿缈昏浆鐨勫悗涓€涓妭鐐广€?
  • 缈昏浆鍚庯紝 start鎸囧悜缈昏浆鍚庨摼琛? 鍖洪棿 锛坰tart锛宔nd锛?/code>涓殑鏈€鍚庝竴涓妭鐐? 杩斿洖 start 鑺傜偣銆?
  • 姝ゆ椂杩橀渶瑕佸皢缈昏浆鍚庣殑鍒嗙粍涓渶鍚庝竴涓妭鐐规寚鍚戜笅涓€涓垎缁勶紝涔熷氨鏄? front.next = cur
  • 涔熷氨鏄浘涓€间负1鑺傜偣鎸囧悜end
銆岀畻娉曚笌鏁版嵁缁撴瀯銆嶉摼琛ㄧ殑9涓熀鏈搷浣?> 
 </figure> 
 <p data-tool=鍦ㄦ潵涓句釜渚嬪瓙锛?code class="mq-425">head=[1,2,3,4,5,6,7,8], k = 3

銆岀畻娉曚笌鏁版嵁缁撴瀯銆嶉摼琛ㄧ殑9涓熀鏈搷浣?> 
 </figure> 
 <p data-tool=鐪嬩笉鍒板氨鑷繁鐢讳釜鍥撅紝鐒跺悗缁撳悎浠g爜澶氱湅鍑犻亶鍚э紝闅鹃灏辫澶氱湅鐫€鍐欏嚑閬嶏紝鑷劧灏辨湁鎰熻浜嗐€?/p>

鍏抽敭鐐瑰垎鏋?/strong>

  • 寤虹珛涓€涓猲ewNode
  • 瀵归摼琛ㄨ繘琛宬涓崟浣嶅垎缁勶紝璁板綍姣忎竴缁勭殑璧峰鍜屾渶鍚庤妭鐐逛綅缃?
  • 瀵规瘡涓€缁勮繘琛岀浉搴旂殑缈昏浆锛岃寰楁洿鎹綅缃?
  • 杩斿洖newNode.next
  var reverseKGroup = (head, k) => {
            
            let  reverseList = (start, end) => {
                let [pre, cur] = [start, start.next],
                    front = cur;
                // 缁堟鏉′欢灏辨槸cur褰撳墠鑺傜偣涓嶈兘绛変簬end鑺傜偣
                
                // 缈昏浆鐨勫璺?/span>
                while( cur !== end) {
                    let next = cur.next
                    cur.next = pre
                    pre = cur
                    cur = next
                }
                front.next = end         // 鏂扮炕杞摼琛ㄩ渶瑕佽繛鎺?涔熷氨鏄痜ront鎸囧悜鍘熸潵鍖洪棿鍚庝竴涓妭鐐?/span>
                start.next = pre        // 鏂扮炕杞殑寮€澶撮渶瑕佽繛鎺tart.next
                return front     // 杩斿洖缈昏浆鍚庨渶瑕佽繛鎺ラ摼琛?涔熷氨鏄痜ront鎸囧悜
            }
            
            let newNode = new ListNode('start')
            newNode.next = head;
            let [start, end] = [newNode,newNode.next],
                count = 0;
            while(end !== null ) {
                count++
                if( count % k === 0) {
                    // k涓妭鐐圭炕杞悗,鍙堥噸鏂板紑濮?杩斿洖鍊煎氨鏄痚nd鑺傜偣鍓嶉潰涓€涓?/span>
                    start = reverseList(start, end.next)
                    end = start.next
                }else{
                    //涓嶆槸涓€涓垎缁勫氨鎸囧悜涓嬩竴涓妭鐐?/span>
                    end = end.next
                }
            }
            return newNode.next
        };

濂藉浼欙紝闈㈣瘯鐨勬椂鍊欙紝瑕佹垜鍐欒繖涓紝涓嶈鎴戠敾鍥剧殑璇濓紝鎴戞娊璞′笉鍑烘潵馃挗馃挗

[浠g爜鐐硅繖閲岎煠璢(https://github.com/daydaylee1227/Blog/blob/master/绠楁硶/閾捐〃/leetcode-K 涓竴缁勭炕杞摼琛?js)


鍚堝苟K涓帓搴忛摼琛ㄢ瓙猸愨瓙

棰樼洰鎻忚堪锛氬悎骞?k 涓帓搴忛摼琛紝杩斿洖鍚堝苟鍚庣殑鎺掑簭閾捐〃銆傝鍒嗘瀽鍜屾弿杩扮畻娉曠殑澶嶆潅搴︺€?/p>

閾炬帴锛歔鍚堝苟K涓帓搴忛摼琛╙(https://leetcode-cn.com/problems/swap-nodes-in-pairs/)

绀轰緥:

杈撳叆:
[
  1->4->5,
  1->3->4,
  2->6
]
杈撳嚭: 1->1->2->3->4->4->5->6

[鍥炴枃閾捐〃]猸?span class="mq-480">

棰樼洰鎻忚堪锛氳鍒ゆ柇涓€涓摼琛ㄦ槸鍚︿负鍥炴枃閾捐〃銆?/p>

閾炬帴锛歭eetcode-鍥炴枃閾捐〃

绀轰緥:

杈撳叆:
[
  1->4->5,
  1->3->4,
  2->6
]
杈撳嚭: 1->1->2->3->4->4->5->6

绀轰緥 1:

杈撳叆: 1->2
杈撳嚭: false

绀轰緥 2:

杈撳叆: 1->2->2->1
杈撳嚭: true

瑙i鎬濊矾锛?/p>

鎵惧埌閾捐〃涓偣锛岀劧鍚庡皢鍚庡崐閮ㄥ垎鍙嶈浆锛屽氨鍙互渚濇姣旇緝寰楀嚭缁撹浜嗐€?/p>

鍏抽敭灏辨槸鎬庝箞鍘绘壘涓偣鍛紵

蹇參鎸囬拡

杩欎釜鍦ㄩ摼琛ㄤ腑搴旂敤澶箍娉涗簡锛屾€濊矾灏辨槸:璁剧疆涓€涓腑闂存寚閽?mid锛屽湪涓€娆¢亶鍘嗕腑锛宧ead 璧颁袱鏍硷紝mid 璧颁竴鏍硷紝褰?head 鍙栧埌鏈€鍚庝竴涓€兼垨鑰呰烦鍑烘椂锛宮id 灏辨寚鍚戜腑闂寸殑鍊笺€?/p>

let mid = head
// 寰幆鏉′欢锛氬彧瑕乭ead瀛樺湪鍒欐渶灏戣蛋涓€娆?br>while(head !== null && head.next !== null) {
    head = head.next.next // 鎸囬拡涓€娆¤蛋涓ゆ牸
    mid = mid.next// 涓棿鎸囬拡涓€娆¤蛋涓€鏍?br>}

閬嶅巻鐨勬椂鍊欓€氳繃杩唬鏉ュ弽杞摼琛紝mid 涔嬪墠鐨?node 閮戒細琚弽杞€備娇鐢ㄨ凯浠f潵鍙嶈浆銆?/p>

while(head !== null && head.next !== null) {
        pre = mid
        mid = mid.next
        head = head.next.next
        pre.next = reversed
        reversed = pre
    }

渚嬪锛?/p>

濂囨暟锛? -> 2 -> 3 -> 2 ->1
閬嶅巻瀹屾垚鍚庯細mid = 3->2->1
reversed = 2->1
鍋舵暟锛? -> 2 -> 2 ->1
閬嶅巻瀹屾垚鍚庯細mid = 2->1
reversed = 2->1

瀹屾暣浠g爜:

  var isPalindrome = function (head{
            if (head === null || head.next === nullreturn true;
            let mid = head,
                pre = null,
                reversed = null// reversed缈昏浆鐨勯摼琛?/span>

            while (head !== null && head.next !== null) {
                // 甯歌缈昏浆鐨勫璺?/span>
                pre = mid
                mid = mid.next
                head = head.next.next
                pre.next = reversed
                reversed = pre
            }
            // 鍒ゆ柇閾捐〃鏁版槸涓嶆槸濂囨暟,鏄殑璇漨id寰€鍚庤蛋涓€浣?/span>
            if (head) mid = mid.next
            while (mid) {
                if (reversed.val !== mid.val) return false
                reversed = reversed.next
                mid = mid.next
            }
            return true
        };

[閾捐〃鐩镐氦]猸?span class="mq-545">

棰樼洰鎻忚堪锛氱粰瀹氫袱涓紙鍗曞悜锛夐摼琛紝鍒ゅ畾瀹冧滑鏄惁鐩镐氦骞惰繑鍥炰氦鐐广€傝娉ㄦ剰鐩镐氦鐨勫畾涔夊熀浜庤妭鐐圭殑寮曠敤锛岃€屼笉鏄熀浜庤妭鐐圭殑鍊笺€傛崲鍙ヨ瘽璇达紝濡傛灉涓€涓摼琛ㄧ殑绗琸涓妭鐐逛笌鍙︿竴涓摼琛ㄧ殑绗琷涓妭鐐规槸鍚屼竴鑺傜偣锛堝紩鐢ㄥ畬鍏ㄧ浉鍚岋級锛屽垯杩欎袱涓摼琛ㄧ浉浜ゃ€?/p>

閾炬帴锛歔leetcode-閾捐〃鐩镐氦]

绀轰緥 1锛?/p>

杈撳叆锛歩ntersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
杈撳嚭锛歊eference of the node with value = 8
杈撳叆瑙i噴锛氱浉浜よ妭鐐圭殑鍊间负 8 锛堟敞鎰忥紝濡傛灉涓や釜鍒楄〃鐩镐氦鍒欎笉鑳戒负 0锛夈€備粠鍚勮嚜鐨勮〃澶村紑濮嬬畻璧凤紝閾捐〃 A 涓?nbsp;[4,1,8,4,5]锛岄摼琛?B 涓?nbsp;[5,0,1,8,4,5]銆傚湪 A 涓紝鐩镐氦鑺傜偣鍓嶆湁 2 涓妭鐐癸紱鍦?B 涓紝鐩镐氦鑺傜偣鍓嶆湁 3 涓妭鐐广€?br>

绀轰緥 2锛?/p>

杈撳叆锛歩ntersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
杈撳嚭锛歊eference of the node with value = 2
杈撳叆瑙i噴锛氱浉浜よ妭鐐圭殑鍊间负 2 锛堟敞鎰忥紝濡傛灉涓や釜鍒楄〃鐩镐氦鍒欎笉鑳戒负 0锛夈€備粠鍚勮嚜鐨勮〃澶村紑濮嬬畻璧凤紝閾捐〃 A 涓?nbsp;[0,9,1,2,4]锛岄摼琛?B 涓?nbsp;[3,2,4]銆傚湪 A 涓紝鐩镐氦鑺傜偣鍓嶆湁 3 涓妭鐐癸紱鍦?B 涓紝鐩镐氦鑺傜偣鍓嶆湁 1 涓妭鐐广€?br>

绀轰緥 3锛?/p>

杈撳叆锛歩ntersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
杈撳嚭锛歯ull
杈撳叆瑙i噴锛氫粠鍚勮嚜鐨勮〃澶村紑濮嬬畻璧凤紝閾捐〃 A 涓?nbsp;[2,6,4]锛岄摼琛?B 涓?nbsp;[1,5]銆傜敱浜庤繖涓や釜閾捐〃涓嶇浉浜わ紝鎵€浠?intersectVal 蹇呴』涓?nbsp;0锛岃€?skipA 鍜?skipB 鍙互鏄换鎰忓€笺€?br>瑙i噴锛氳繖涓や釜閾捐〃涓嶇浉浜わ紝鍥犳杩斿洖 null銆?br>

鎬濊矾锛?/p>

  • 璁剧疆涓や釜鎸囬拡,姣忔潯鎸囬拡璧板畬鑷繁鐨勮矾鍚?鎸囧悜鍙﹀涓€涓摼琛?閭d箞涓や釜鑺傜偣鐩哥瓑鐨勮瘽锛屼竴瀹氭槸鍚屼竴涓偣銆?
  • 鍥犱负涓や釜鎸囬拡璧扮殑璺濈鏄竴鏍风殑,鑰屼笖姣忔閮藉墠杩?锛岃窛绂荤浉绛?閫熷害鐩稿悓,濡傛灉鐩哥瓑锛屼竴瀹氭槸鍚屼竴涓偣銆?
var getIntersectionNode = function (headA, headB{
    let p1 = headA,
        p2 = headB;
    while (p1 != p2) {
        p1 = p1 === null ? headB : p1.next
        p2 = p2 === null ? headA : p2.next
    }
    return p1
};

鉂わ笍 鎰熻阿澶у

鑱氱劍鍏ㄦ爤锛屼笓娉ㄥ垎浜?TypeScript銆乄eb API銆佸墠绔灦鏋勭瓑鎶€鏈共璐с€?/span>


以上是关于的主要内容,如果未能解决你的问题,请参考以下文章

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数

VSCode自定义代码片段8——声明函数

(c)2006-2024 SYSTEM All Rights Reserved IT常识