鏈表分為單鏈表,雙向鏈表和循環(huán)鏈表。? ? ? ? ? ??
不同于數(shù)組。鏈表中的節(jié)點(diǎn)在內(nèi)存中的分布不是連續(xù)的,而是散落分布的。鏈表用于插入和查詢的效率更高,相反,數(shù)組更多用于查詢。js中自定義鏈表:
function ListNode(val, next) {
????this.val = (val===undefined ? 0 : val)
????this.next = (next===undefined ? null : next)
}

203.移除鏈表元素??
#1 自己看到題目的第一想法??
鏈表的移除操作,即如果當(dāng)前的 value 為 target,那么將前一個節(jié)點(diǎn)的 next 指向下一個節(jié)點(diǎn),但是關(guān)于如何實(shí)現(xiàn)代碼,我是不太有思路的。例如如何循環(huán)整個鏈表?我個人覺得 可能是 while (next != null)
#2 看完代碼隨想錄之后的想法? ??
自己對于鏈表的理解還是非常的表層,同時非常不熟練,此處附上 js 中如何刪除元素的代碼:
var removeElements = function(head, val) {
? ? const ret = new ListNode(0, head);
? ? let cur = ret;
? ? console.log(cur)
? ? while(cur.next) {
? ? ? ? console.log(cur, cur.next)
? ? ? ? // if next one is the val, then delete the next one
? ? ? ? if(cur.next.val === val) {
? ? ? ? ? ? cur.next =? cur.next.next;
? ? ? ? ? ? continue;
? ? ? ? }
? ? ? ? // if next one is the val, then delete this one
? ? ? ? cur = cur.next;
? ? }
? ? return ret.next;
};
#3 收獲? ??
鏈表的刪除在對于head的處理上是需要單獨(dú)處理的,通常的做法是在頭部加一個暫時的元素,最后return的時候再刪掉即可。
707.設(shè)計(jì)鏈表?
#1 自己看到題目的第一想法
#2 看完代碼隨想錄之后的想法??
#3 收獲? ?
#4 類似題目??
206.反轉(zhuǎn)鏈表?
#1 自己看到題目的第一想法
#2 看完代碼隨想錄之后的想法? ?
#3 收獲? ??
#4 類似題目?