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

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

1596745200

Hey everyone. Welcome back to our Linked List session and welcome to those who just joined us. Today, we are going to learn how to insert element in the end of the list and get element by index from our list. To my opinion, step by step we are getting better and today we will be in one step better again. Let’s do it.

Previous blog about remove first and last element:

If somebody decide to read from the beginning:

**Function → “insertLast()”**

Inserts a new node with provided data at the end of the chain.

```
const list = new LinkedList();
list.insertFirst('a');
list.insertFirst('b');
list.insertLast('c');
list.getLast(); //returns node with data 'c'
```

We have already three nodes in the list and we would like to add one more. Of course we can iterate through all and find the last and connect to the last new element. But to my opinion, we don’t need to do it again. We already have the method which can help us to find the last element and I guess it will be the best idea. The method is getLast(). Also we need to check if our list is not empty. If it is empty we need to add just as a first element.

Three element at the beginning

Add orange element to our list

InsertLast function

Example

Result

**Function → “getAt()”**

Returns the node at the provided index.

#data-structures #software-development #javascript #linked-lists #algorithms #algorithms

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

1600161631

Cycle detection is the algorithmic problem of finding a cycle in a sequence of iterated function values, for example, the classic linked list loop detection problem. There are different solution. For the following *Leetcode* example, I’ll explain true solutions.

Given `head`

, the head of a linked list, determine if the linked list has a cycle in it.

There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the `next`

pointer. Internally, `pos`

is used to denote the index of the node that tail’s `next`

pointer is connected to. **Note that **.

`**pos**`

is not passed as a parameter#javascript #floyd-algorithm #linked-lists #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