鍊錶高頻演算法題

2021-10-07 15:19:15 字數 1618 閱讀 2438

class

solutionif(

!l2)

listnode* dummy =

new listnode;

listnode* res = dummy;

res-

>next =

null

;while

(l1 && l2)

else}if

(l1)

if(l2)

return dummy-

>next;}}

;

class

solution

listnode* pre = head;

listnode* cur = head-

>next;

pre-

>next =

null

;while

(cur)

return pre;}}

;

class

solution

stack<

int> s;

listnode *slow = head;

listnode *fast = head;

s.push

(slow-

>val)

;while

(fast-

>next && fast-

>next-

>next)

//奇數

if(fast-

>next ==

null

) listnode *cur = slow;

while

(cur-

>next)

s.pop();

}return

true;}

};

class

solution

while

(l2)

listnode* longlist = heada;

listnode* shortlist = headb;

if(lena < lenb)

for(

int i =

0; i <

abs(lena - lenb)

; i++

)while

(longlist && shortlist)

longlist = longlist-

>next;

shortlist = shortlist-

>next;

}return

null;}

};

環形鍊錶:

如果鍊錶是有環的,快指標和慢指標最後會同時指向同乙個節點。指標從 相遇點出發和從鍊錶的頭出發,最後會遍歷相同數目的節點後在環的入口處相遇。

listnode *

detectcycle

(listnode *head)

else

if(fast == slow)

return slow;}}

return0;

}

LeetCode高頻題 鍊錶(一)

我開了乙個leetcode會員,選擇了一些高頻率題目。這個系列是希望幫助大家每天花30分鐘的題目了解面試高頻題,讓大家面試更加游刃有餘。本期我們開始講解鍊錶的題目,先介紹幾道簡單題熱熱身。本期所用的鍊錶的資料結構均如下 public class listnode 題意 刪除鍊錶中等於給定值 val ...

LeetCode高頻題 鍊錶(四)

我們講解的題目都是leetcode上經典的題目,而且我們的解答一定也是最簡單最經典的。今天帶來兩道關於鍊錶的經典題。本期所用的鍊錶的資料結構均如下 public class listnode 題意 給定乙個單鏈表 l l0 l1 ln 1 ln 將其重新排列後變為 l0 ln l1 ln 1 l2 ...

面試高頻演算法題 單鏈表系列(1) 倒置鍊錶

反轉鍊錶是一道比較基礎的演算法題啦,但是確實面試高頻演算法題,同學前兩天位元組一面就被問到了這個問題,寫篇博文來記錄一下 首先上題目 思路都寫在注釋裡啦,所以直接上 啦 var reverselist function head return revhead 執行成功截圖 下面我們來用 的方法具體介...