1603450800

# Building a Singly Linked List

Most likely you arrived at this article because your preparation for an interview has lead you down a LeetCode rabbit hole. Linked lists are a favorite of many interviewers to test an interviewees understanding of basic data structures as well as your problem solving skills. Whether you’re just a bit rusty from school or this is your first time encountering linked lists they can be a bit tricky to understand at first.

As with most problems it is helpful to start with a diagram. A linked list is a linear data structure which can be thought of as a chain, where each link in a chain is connected to the next link. In a linked list, each link in the chain will be represented as a node.

In a singly linked list, each node contains some data and then a pointer to the next node. For a doubly linked list there would also be a pointer to the previous node. The first node in the list is conventionally referred to as the head and the final node’s next pointer refers to nothing. With this diagram constructed it is now time to start coding up a singly linked list in JavaScript.

### Coding a Node Class

Since a singly linked list consists of many notes it makes sense to create a class `Node` that way a new node can be easily and consistently instantiated. At the instantiation of a new node the data it will contain should be know but the next node might not be known.

Figure 2: JavaScript Node Class

A doubly linked list node class would look very similar to Figure 2 but with an additional property of `previous` that would also initialize to `null` as it may not be known or it could be the head node.

1604028420

## Editing a Singly Linked List

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.

1624320600

## What Is a Linked List?

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:

• Data (such as a number)
• Pointer to the next node

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

1605854137

## JavaScript Algorithm: How to Reverse Linked List

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.

1603450800

## Building a Singly Linked List

Most likely you arrived at this article because your preparation for an interview has lead you down a LeetCode rabbit hole. Linked lists are a favorite of many interviewers to test an interviewees understanding of basic data structures as well as your problem solving skills. Whether you’re just a bit rusty from school or this is your first time encountering linked lists they can be a bit tricky to understand at first.

As with most problems it is helpful to start with a diagram. A linked list is a linear data structure which can be thought of as a chain, where each link in a chain is connected to the next link. In a linked list, each link in the chain will be represented as a node.

In a singly linked list, each node contains some data and then a pointer to the next node. For a doubly linked list there would also be a pointer to the previous node. The first node in the list is conventionally referred to as the head and the final node’s next pointer refers to nothing. With this diagram constructed it is now time to start coding up a singly linked list in JavaScript.

### Coding a Node Class

Since a singly linked list consists of many notes it makes sense to create a class `Node` that way a new node can be easily and consistently instantiated. At the instantiation of a new node the data it will contain should be know but the next node might not be known.

Figure 2: JavaScript Node Class

A doubly linked list node class would look very similar to Figure 2 but with an additional property of `previous` that would also initialize to `null` as it may not be known or it could be the head node.