The reorderList function will return the head of the reordered linked list given a linked list as input

Reorder Linked Lists

Problem Statement We have to implement the reorderList function that takes the head node of a linked list as input and reorders its nodes in the format specified below. Given an input list like the following: $$Node_0 \rightarrow Node_1 \rightarrow Node_2 \rightarrow \dots \rightarrow Node_{(n-2)} \rightarrow Node_{(n-1)} \rightarrow Node_n$$ the reorderList function should reorder its nodes as: $$Node_0 \rightarrow Node_n \rightarrow Node_1 \rightarrow Node_{(n-1)} \rightarrow Node_2 \rightarrow Node_{(n-2)} \rightarrow \dots$$...

February 15, 2024 · 7 min · Avnish
The mergeTwoLists function will return the head of the merged linked list given two sorted linked lists as input

Merge Two Sorted Linked Lists

Problem Statement We have to implement the mergeTwoLists function that takes the head nodes of two sorted linked lists as input and returns the head node of the merged linked list as the output. Optimal Solution To merge both linked lists we can use the two-pointer approach by maintaining an iterator on both linked lists and comparing their values. The smaller value will be selected and inserted at the end of a new list....

January 29, 2024 · 4 min · Avnish
The reverseList function will return the head of the reversed linked list

Reversing Linked Lists

Problem Statement We have to implement the reverseList function that takes the head node of a linked list as an input and returns the head node of the reversed linked list in the output. Brute Force Solution If we iterate over the input linked list and insert its value at the beginning of a new list the result would be a reversed linked list. Psuedo-code for the Brute Force Solution reversedLinkedList = LinkedList() temp = linked_list....

January 26, 2024 · 4 min · Avnish
Linked Lists

Linked Lists

The linked list data structure is used to store sequential data using nodes. A node contains a value and the memory address of the next node. Memory for a new node is allocated dynamically i.e. nodes are stored in the next available memory location. Unlike arrays where contiguous blocks of memory are allocated during declaration. The first element in a linked list is marked by its head pointer. Here is an implementation of Linked List in Go...

November 17, 2023 · 10 min · Avnish
The longestConsecutive function will return the length of the longest sequence if consecutive numbers in the input array

Finding the Longest Consecutive Sequence in an Array

Problem Statement We have to implement the longestConsecutive function that takes an integer array as input and returns the length of the longest sequence of consecutive integers. For example, in array [4, 2, 7, 8, 1, 5, 6, 0] we have two sequences of consecutive integers: [0, 1, 2] and [4, 5, 6, 7, 8]. The longest sequence ([4, 5, 6, 7, 8]) has length 5. Brute Force Solution It would be easier to find consecutive sequences if the input array is sorted....

November 9, 2023 · 5 min · Avnish