MetaID: 921a309d-ef20-4e36-b492-9df5a6fc906e
views
101 days ago
Conversation
Js reverse linked list and priority queue
Reversing a Linked List in JavaScript
To reverse a singly linked list in JavaScript, you can use the following implementation:
JavaScript
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
reverse() {
let prev = null;
let current = this.head;
while (current !== null) {
let next = current.next;
current.next = prev;
prev = current;
current = next;
}
this.head = prev;
}
append(val) {
let newNode = new Node(val);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
printList() {
let current = this.head;
let output = '';
while (current) {
output += current.val + ' ';
current = current.next;
}
console.log(output);
}
}
// Example usage
let ll = new LinkedList();
ll.append(1);
ll.append(2);
ll.append(3);
console.log("Original List:");
ll.printList();
ll.reverse();
console.log("Reversed List:");
ll.printList();Priority Queue in JavaScript
A priority queue can be implemented using a min-heap or max-heap. Here's a basic implementation of a min-priority queue in JavaScript:
JavaScript
class PriorityQueue {
constructor() {
this.heap = [];
}
// Add element with priority
enqueue(val, priority) {
this.heap.push({ val, priority });
this.heapifyUp();
}
// Remove and return element with highest priority (lowest number)
dequeue() {
if (this.heap.length === 0) return null;
if (this.heap.length === 1) return this.heap.pop();
let min = this.heap[0];
this.heap[0] = this.heap.pop();
this.heapifyDown();
return min;
}
heapifyUp() {
let idx = this.heap.length - 1;
let parentIdx = Math.floor((idx - 1) / 2);
while (idx > 0 && this.heap[idx].priority < this.heap[parentIdx].priority) {
[this.heap[idx], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[idx]];
idx = parentIdx;
parentIdx = Math.floor((idx - 1) / 2);
}
}
heapifyDown() {
let idx = 0;
let leftChild = 2 * idx + 1;
while (leftChild < this.heap.length) {
let minChild = leftChild;
let rightChild = leftChild + 1;
if (rightChild < this.heap.length && this.heap[rightChild].priority < this.heap[leftChild].priority) {
minChild = rightChild;
}
if (this.heap[idx].priority <= this.heap[minChild].priority) break;
[this.heap[idx], this.heap[minChild]] = [this.heap[minChild], this.heap[idx]];
idx = minChild;
leftChild = 2 * idx + 1;
}
}
}
// Example usage
let pq = new PriorityQueue();
pq.enqueue('low', 1);
pq.enqueue('high', 0);
pq.enqueue('medium', 2);
console.log(pq.dequeue()); // { val: 'high', priority: 0 }
console.log(pq.dequeue()); // { val: 'low', priority: 1 }
console.log(pq.dequeue()); // { val: 'medium', priority: 2 }Meta AI Response: Reversing a Linked List in JavaScript To reverse a singly linked list in JavaScript, you can use the following implementation: class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
}
reverse() {
let prev = null;
let current = this.head;
while (current !== null) {
let next = current.next;
current.next = prev;
prev = current;
current = next;
}
this.head = prev;
}
append(val) {
let newNode = new Node(val);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
printList() {
let current = this.head;
let output = '';
while (current) {
output += current.val + ' ';
current = current.next;
}
console.log(output);
}
}
// Example usage
let ll = new LinkedList();
ll.append(1);
ll.append(2);
ll.append(3);
console.log("Original List:");
ll.printList();
ll.reverse();
console.log("Reversed List:");
ll.printList(); Priority Queue in JavaScript A priority queue can be implemented using a min-heap or max-heap. Here's a basic implementation of a min-priority queue in JavaScript: class PriorityQueue {
constructor() {
this.heap = [];
}
// Add element with priority
enqueue(val, priority) {
this.heap.push({ val, priority });
this.heapifyUp();
}
// Remove and return element with highest priority (lowest number)
dequeue() {
if (this.heap.length === 0) return null;
if (this.heap.length === 1) return this.heap.pop();
let min = this.heap[0];
this.heap[0] = this.heap.pop();
this.heapifyDown();
return min;
}
heapifyUp() {
let idx = this.heap.length - 1;
let parentIdx = Math.floor((idx - 1) / 2);
while (idx > 0 && this.heap[idx].priority < this.heap[parentIdx].priority) {
[this.heap[idx], this.heap[parentIdx]] = [this.heap[parentIdx], this.heap[idx]];
idx = parentIdx;
parentIdx = Math.floor((idx - 1) / 2);
}
}
heapifyDown() {
let idx = 0;
let leftChild = 2 * idx + 1;
while (leftChild < this.heap.length) {
let minChild = leftChild;
let rightChild = leftChild + 1;
if (rightChild < this.heap.length && this.heap[rightChild].priority < this.heap[leftChild].priority) {
minChild = rightChild;
}
if (this.heap[idx].priority <= this.heap[minChild].priority) break;
[this.heap[idx], this.heap[minChild]] = [this.heap[minChild], this.heap[idx]];
idx = minChild;
leftChild = 2 * idx + 1;
}
}
}
// Example usage
let pq = new PriorityQueue();
pq.enqueue('low', 1);
pq.enqueue('high', 0);
pq.enqueue('medium', 2);
console.log(pq.dequeue()); // { val: 'high', priority: 0 }
console.log(pq.dequeue()); // { val: 'low', priority: 1 }
console.log(pq.dequeue()); // { val: 'medium', priority: 2 }