列表節點類
class ListNode {
constructor(data) {
this.data = data
this.next = null
}
}
鏈接列表類
class LinkedList {
constructor(head = null) {
this.head = head
}
}
創建鏈接列表
let node1 = new ListNode(2)
let node2 = new ListNode(5)
node1.next = node2
let list = new LinkedList(node1)
console.log(list.head.next.data) //returns 5
size() {
let count = 0;
let node = this.head;
while (node) {
count++;
node = node.next
}
return count;
}
const ListFunctions = {
display: (linkedList) => {
items = [];
if (linkedList.head === null) {
return "The list is empty";
} else {
let currNode = linkedList.head;
while (currNode.next !== null) {
items.push(currNode.data);
currNode = currNode.next;
}
items.push(currNode.data);
}
return `Linked List Items: ${items.join("->")}`;
}
isEmpty: (linkedList) => {
if (linkedList.head === null) {
return `This list is empty`;
}
return `There are items in this list`;
}
find(item) {
let currNode = this.head;
if (!this.head) {
return null;
}
while (currNode.data !== item) {
if (currNode.next === null) {
return null;
} else {
currNode = currNode.next;
}
}
return currNode;
}
}
clear() {
this.head = null;
}
remove(item) {
if (!this.head) {
return null;
}
if (this.head.data === item) {
this.head = this.head.next;
return;
}
let currNode = this.head;
let previousNode = this.head;
while (currNode !== null && currNode.data !== item) {
previousNode = currNode;
currNode = currNode.next;
}
if (currNode === null) {
return;
}
previousNode.next = currNode.next;
}
insertFirst(item) {
this.head = new _Node(item, this.head);
}
insertLast(item) {
if (this.head === null) {
this.insertFirst(item);
} else {
let tempNode = this.head;
while (tempNode.next !== null) {
tempNode = tempNode.next;
}
tempNode.next = new _Node(item, null);
}
}
insertBefore(item, key) {
if (!this.head) {
return;
}
if (this.head.data === key) {
this.insertFirst(item);
return;
}
let currNode = this.head;
let previousNode = this.head;
while (currNode !== null && currNode.data !== key) {
previousNode = currNode;
currNode = currNode.next;
}
if (currNode === null) {
console.log(`key value not found`);
return;
}
previousNode.next = new _Node(item, currNode);
}
insertAfter(item, key) {
if (!this.head) {
return;
}
if (this.head.data === key) {
this.insertFirst(item);
return;
}
let currNode = this.head;
let nextNode = this.head;
while (currNode !== null && currNode.data !== key) {
currNode = nextNode;
nextNode = nextNode.next;
}
if (currNode === null) {
console.log(`key value not found`);
return;
}
currNode.next = new _Node(item, nextNode);
}
getLast() {
let lastNode = this.head;
if (lastNode) {
while (lastNode.next) {
lastNode = lastNode.next
}
}
return lastNode
}
getFirst() {
return this.head;
}
findPrevious: (linkedList, key) => {
if (linkedList.head === null) {
return null;
}
let prevNode;
let currNode = linkedList.head;
while (currNode.data !== key) {
prevNode = currNode;
currNode = currNode.next;
}
return `The previous node is ${prevNode.data}`;
}
在這些演習中,您將練習創建一個鏈接的列表,實施其核心和一些補充操作。您還將使用鏈接列表來解決面試問題。不要忘記評估這些練習中的每一個。通過說明1個或更多樣本輸入和輸出來開始每個問題。
從頭開始編寫一個鏈接的列表類和這些核心功能(插入First,insertlast,remove,find)。
在您的鏈接列表類中實現以下功能。請注意,這些應該是免費功能,而不是鏈接列表類的方法,因此在鏈接列表類中實現它們。使用練習1中創建的列表測試每個功能。
分析以下功能(不在IDE中運行它)以確定要解決的問題。該算法的時間複雜性是多少?
function WhatDoesThisProgramDo(lst) {
let current = lst.head;
while (current !== null) {
let newNode = current;
while (newNode.next !== null) {
if (newNode.next.value === current.value) {
newNode.next = newNode.next.next;
}
else {
newNode = newNode.next;
}
}
current = current.next;
}
}
編寫算法以扭轉鏈接列表。算法的時間複雜性應為線性(O(n))。在此練習中,請注意,我們不要求您僅在反向中打印鏈接列表,也不會使用另一個鏈接列表以相反的順序存儲該值。您的程序應扭轉給定單鏈接列表的方向。換句話說,所有指針都應向後指向。獎勵:使用遞歸和迭代算法解決此問題。
編寫一個算法以從鏈接列表的末尾找到第三元素。請注意,您可能很想在鏈接列表類中添加長度屬性。長度屬性不是鏈接列表的典型屬性,因此不要對提供給您的鏈接列表類進行任何修改。
編寫算法以找到鏈接列表的中間元素。請注意,您可能很想在鏈接列表類中添加長度屬性。長度屬性不是鏈接列表的典型屬性,因此不要對提供給您的鏈接列表類進行任何修改。此外,使用size()函數找到鏈接列表的大小,然後將其除以一半,將找不到鏈接列表的正確中間。因此,不要使用這兩種方法。
編寫一種算法以查找鏈接列表是否具有周期(即,列表中的節點是否具有下一個值指向列表中的一個節點的下一個值)。對於此練習,創建一個名稱Cyclelist的鏈接列表。請確保將節點插入列表中,以使其具有一個週期。然後使用Cyclelist功能測試您的程序。
實現雙重鏈接列表。雙鏈接列表的主要功能將是插入(首先,最後,之前,之後和at),刪除並查找。編寫一個函數aintll,並在其中創建雙重鏈接的列表DLL,然後添加以下項目:Aquaria,caprica,gemenon,picon,picon,sagittaron。
給定上面的雙重鏈接列表,編寫一個逆轉雙重鏈接列表的程序。這種實現與逆轉單鏈接列表有何不同?