# Linked Lists — 2

> A doubly linked list is a linked data structure that consists of a set of sequentially linked records called **nodes**. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes.

> *⚠️* If you wan’t to learn more about linked-lists [**click here**](https://en.wikipedia.org/wiki/Doubly_linked_list)

## Core <a href="#a873" id="a873"></a>

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7Yl1gF8U3vBKQJFx9h%2FScreen%20Shot%202020-05-18%20at%205.52.26%20AM.png?alt=media\&token=6a0714b1-c635-4f66-acb6-0423ed3eae64)

## ⚜️ The list <a href="#id-1e1f" id="id-1e1f"></a>

1. *Append*
2. *AppendAt*
3. *Remove*
4. *RemoveAt*
5. *Reverse*
6. *Swap*
7. *IsEmpty & Length*
8. *Traverse*
9. *Display*
10. *Search*

## Lets’ begin!! <a href="#dbd8" id="dbd8"></a>

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7YlAANg_iDGK6wgcsF%2FScreen%20Shot%202020-05-18%20at%205.52.59%20AM.png?alt=media\&token=8a8bccee-f3fd-4a4c-92df-3f83f4e3e8fb)

Initial List

> ❗*️*All the code snippets you will see below are just Pseudo code.

### 🔅 Append <a href="#acc1" id="acc1"></a>

```
append( item ) {
  let node = new Node( item );  if(!this.head) {
    this.head = node;
    this.tail = node;
  } else {
    node.prev = this.tail;
    this.tail.next = node;
    this.tail = node
  }
}
```

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7YlIz1R4rVosEX_97g%2FScreen%20Shot%202020-05-18%20at%205.53.38%20AM.png?alt=media\&token=f9d2ee36-7720-4e09-b7c4-a04dcb364223)

Append Operation

### 🔅 AppendAt <a href="#b871" id="b871"></a>

```
appendAt( pos, item ) {
   let current = this.head;
   let counter = 1;
   let node = new Node( item );
   if( pos == 0 ) {
     this.head.prev = node
     node.next = this.head
     this.head = node
   } else {
     while(current) {
      current = current.next;
      if( counter == pos ) {
        node.prev = current.prev
        current.prev.next = node
        node.next = current
        current.prev = node
      }
      counter++
    }
  }
}
```

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7YlQItYgNpA1y2YdJN%2FScreen%20Shot%202020-05-18%20at%205.54.05%20AM.png?alt=media\&token=3b89ddb7-3fc4-470a-b2ac-14357c3d0f1e)

AppendAt Operation

### 🔅 Remove <a href="#id-7a05" id="id-7a05"></a>

```
remove( item ) {
  let current = this.head;
  while( current ) {
    if( current.data === item ) {
      if( current == this.head && current == this.tail ) {
        this.head = null;
        this.tail = null;
      } else if ( current == this.head ) {
        this.head = this.head.next
        this.head.prev = null
      } else if ( current == this.tail ) {
        this.tail = this.tail.prev;
        this.tail.next = null;
      } else {
        current.prev.next = current.next;
        current.next.prev = current.prev;
      }
   }
   current = current.next
  }
}
```

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7YlXPnRj2VWfm_l2Q1%2FScreen%20Shot%202020-05-18%20at%205.54.34%20AM.png?alt=media\&token=a09fa9dd-c5a9-4a7a-82d0-4e6e910d6c90)

Remove Operation

### 🔅 RemoveAt <a href="#id-468d" id="id-468d"></a>

```
removeAt( pos ) {
  let current = this.head;
  let counter = 1;
  if( pos == 0 ) {
   this.head = this.head.next;
   this.head.prev = null;
  } else {
   while( current ) {
    current = current.next
    if ( current == this.tail ) {
     this.tail = this.tail.prev;
     this.tail.next = null;
    } else if( counter == pos ) {
     current.prev.next = current.next;
     current.next.prev = current.prev;
     break;
    }
    counter++;
   }
  }
}
```

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7YlccCIqU_mYHipj38%2FScreen%20Shot%202020-05-18%20at%205.55.01%20AM.png?alt=media\&token=08e5281c-a7b5-435f-9b74-2fbf56fc70bc)

RemoveAt Operation

### 🔅 Reverse <a href="#id-76dc" id="id-76dc"></a>

```
reverse(){
  let current = this.head;
  let prev = null;
  while( current ){
   let next = current.next
   current.next = prev
   current.prev = next
   prev = current
   current = next
  }
  this.tail = this.head
  this.head = prev
}
```

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7YliZ-bAXwBRwXcq0P%2FScreen%20Shot%202020-05-18%20at%205.55.25%20AM.png?alt=media\&token=ef473143-c333-4df0-833e-f597a199e30a)

Reverse Operation

### 🔅 Swap <a href="#id-4d6a" id="id-4d6a"></a>

```
swap( nodeOne, nodeTwo ) {
   let current = this.head;
   let counter = 0;
   let firstNode;
   while( current !== null ) {
     if( counter == nodeOne ){
       firstNode = current;
     } else if( counter == nodeTwo ) {
       let temp = current.data;
       current.data = firstNode.data;
       firstNode.data = temp;
     }
     current = current.next;
     counter++;
   }
   return true
}
```

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7Ylo0j07L_YuRhBX4d%2FScreen%20Shot%202020-05-18%20at%205.55.48%20AM.png?alt=media\&token=efd1bbd1-dedd-4416-af5a-c519c13fb081)

Swap Operation

### 🔅 IsEmpty & Length <a href="#id-620d" id="id-620d"></a>

```
length() {
  let current = this.head;
  let counter = 0;
  while( current !== null ) {
   counter++
   current = current.next
  }
  return counter;
}isEmpty() {
  return this.length() < 1
}
```

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7YltJBsflOz1DpWWwr%2FScreen%20Shot%202020-05-18%20at%205.56.11%20AM.png?alt=media\&token=79d743aa-6f0e-4cbd-a4ef-18cdaac4ea3d)

IsEmpty & Length Operation

### 🔅 Traverse <a href="#id-11ea" id="id-11ea"></a>

```
traverse( fn ) {
   let current = this.head;
   while( current !== null ) {
    fn(current)
    current = current.next;
   }
   return true;
}
```

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7YlyoETj-sQjsqL-XK%2FScreen%20Shot%202020-05-18%20at%205.56.33%20AM.png?alt=media\&token=3243e0f8-517d-472d-928e-2d8ff903d7d4)

Traverse Operation

### 🔅 TraverseReverse <a href="#id-8048" id="id-8048"></a>

```
traverseReverse( fn ) {
  let current = this.tail;
  while( current !== null ) {
   fn(current)
   current = current.prev;
  }
  return true;
}
```

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7Ym3gXXgGpbuP5qGeq%2FScreen%20Shot%202020-05-18%20at%205.56.56%20AM.png?alt=media\&token=4c768e9b-4464-4a7e-b5e6-f85690cb8dd8)

TraverseReverse Operation

### 🔅 Search <a href="#id-7195" id="id-7195"></a>

```
search( item ) {
  let current = this.head;
  let counter = 0;  while( current ) {
    if( current.data == item ) {
     return counter
    }
    current = current.next
    counter++
  }
  return false;
}
```

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7YmA0QZDHdoqeYJnXq%2FScreen%20Shot%202020-05-18%20at%205.57.24%20AM.png?alt=media\&token=3ec878fd-6f84-4b55-8599-bb841f81905d)

Search Opeartion

![](https://453584167-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-M0tzdQoYXoPhCiqfqxs%2F-M7YjhZ1ArxA1BZg-825%2F-M7Ymg8qwN51nWAcfoJO%2FScreen%20Shot%202020-05-18%20at%205.57.48%20AM.png?alt=media\&token=0ba13918-69c1-4e15-b232-39f6decba881)

## Practice <a href="#id-24e2" id="id-24e2"></a>

1. GeekForGeeks [*Double Linked List*](https://www.geeksforgeeks.org/tag/doubly-linked-list/)
2. HackerRank [*Linked Lists*](https://www.hackerrank.com/domains/data-structures/linked-lists/2)
