1603237680
This week, we will cover another popular technical interview question from LeetCode’s Top Interview Questions List; Reverse Linked List:
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
This article will not give many details about linked lists, but here is a very quick refresher: Linked lists are data structures that represent a series of nodes where each node contains two pieces of information: the value of the node and a pointer/reference to the next node in the list. The beginning of the list is called the head and the node at the end of the list is referred to as the tail, which points to the empty value; null.
https://www.geeksforgeeks.org/data-structures/linked-list/
The main benefit of a linked list over an array is an easier insertion or removal of nodes in the list. On the other hand, random access to the data is not allowed because the linked lists do not have indexes, unlike arrays.
#javascript #interview #linked-lists #algorithms #programming
1605854137
When given a singly linked list how can we manage to reverse it? Like the following example, if the input linked list is 1->2->3->4->5->NULL, can we reverse it into the output as 5->4->3->2->1->NULL?
Example Input
Example Output
Each node of Linked list will have two attributes: value & next, and linked list will have head, tail and length attribute.
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
https://gist.github.com/GAierken/6e15c82f5e3457fa2d16e29400e130aa
There are two approaches to reverse a singly linked list: iterative and recursive.
#javascript #recursion #singly-linked-list #reverse-linked-list #iteration
1595641189
In this article, we are going to cover the concept of linked lists, what linked lists are and how to implement one in your project. This article covers the basic way to implement a linked list in JavaScript.
In this article, i will assume that you already know or have a basic knowledge of javascript and general programming concepts like strings and arrays.
A linked list is just another data structure like arrays , stacks or queues. A linked list unlike an array contains nodes where each node has a data part which stores our data and a next part or a reference to the next node in the list.
The nodes are connected together through links. In a more simple term, a linked list is a collection of nodes which are connected to each other through links. These nodes have two parts. The first part stores our data while the second part stores the address or link to the next node in the list.
Consider the diagram below.
The first node in a list is usually called a head. The head links to the next node through the next link and it continues till the last node which is not pointing to any other node, so the next value of the last node becomes NULL.
Linked lists cannot be accessed randomly. I.e you cannot access data with it’s index (this can be manipulated but it’s not like we do with arrays). In linked lists, we refer to the memory location Address, not the index location.
Now that we have a better understanding of linked lists. Let’s go on and implement it using javascript.
The first thing we are going to do is to create a node constructor function or class. I will be using ES6 classes in the article.
class Node{
constructor(data, next = null){
this.data = data;
this.next = next;
}
#javascript-tips #programming #linked-lists #javascript #javascript-development
1624320600
A linked list is a basic data structure. It is a collection of nodes that connects each node to the next node with a pointer.
Each node consists of two items:
Here is an illustration of a linked list
A linked list forms a chain of nodes. Each node holds data and points to the next node. Image by the author.
#programming #coding #python #how to create a linked list in python #create a linked list #linked list
1604028420
In the previous “Data Structures in JavaScript” discussion, we walked through the methodology used to construct a node class which represent each “link” in the “chain” that is a linked list. From there we discussed how to construct a singly linked list from a an array. Building on the previous discussion, we will now dive into editing that singly linked list. If you are not familiar with the code and terminology covered in the previous article it is recommended you review Building a Singly Linked List.
With a linked list there are three ways to edit the list: update a nodes data property, add a node to the list or delete a node from the list. In all these cases, we will want to access a specific place in the singly linked list. Depending on the situation, you might want to edit the nth node from the head of the list, the nth node from the end of the list, or a node of a particular data value. For this discussion, we will assume that we are interested in editing the nth Node from the head of the list. With this in mind, we can step through a singly linked list from the head to the nth a node using a simple for loop as shown in Figure 1.
#javascript #singly-linked-list #linked-lists #data-structures #algorithms
1603237680
This week, we will cover another popular technical interview question from LeetCode’s Top Interview Questions List; Reverse Linked List:
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
This article will not give many details about linked lists, but here is a very quick refresher: Linked lists are data structures that represent a series of nodes where each node contains two pieces of information: the value of the node and a pointer/reference to the next node in the list. The beginning of the list is called the head and the node at the end of the list is referred to as the tail, which points to the empty value; null.
https://www.geeksforgeeks.org/data-structures/linked-list/
The main benefit of a linked list over an array is an easier insertion or removal of nodes in the list. On the other hand, random access to the data is not allowed because the linked lists do not have indexes, unlike arrays.
#javascript #interview #linked-lists #algorithms #programming