### Design Linked List - The Coding Shala

Home >> Data Structure >> Design Linked List

Problem:

Design your implementation of the linked list. You can choose to use them a singly linked list or the doubly linked list. A node in a singly linked list should have two attributes: Val and next. Val is the value of the current node, and next is a pointer/reference to the next node. If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

get(index): Get the value of the index-th node in the linked list. If the index is invalid, return -1.

addAtHead(val): Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.

addAtTail(val): Append a node of value val to the last element of the linked list.

addAtIndex(index, val): Add a node of value val before the index-th node in the linked list. If the index equals the length of the linked list, the node will be appended to the end of the linked list. If the index is greater than the length, the node will not be inserted. If the index is negative, the node will be inserted at the head of the list.

deleteAtIndex(index) : Delete the index-th node in the linked list, if the index is valid.

Example:

Note:

All values will be in the range of [1, 1000].
The number of operations will be in the range of [1, 1000].

### Design Linked List Java Solution

Approach

Java

```class MyLinkedList {

class Node{
int value;
Node next;
Node(int val){
this.value = val;
}
}

int size;

/** Initialize your data structure here. */
size = 0;
}

/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
if(index < 0 || index >= size) return -1;
for(int i = 0; i<=size; i++){
if(i == index){
return tmp.value;
}
tmp = tmp.next;
}
return -1;
}

/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
Node nw = new Node(val);
size++;
}

/** Append a node of value val to the last element of the linked list. */
Node nw = new Node(val);
for(int i = 0; i<size-1;i++) tmp = tmp.next;
tmp.next = nw;
nw.next = null;
size++;
}

/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
public void addAtIndex(int index, int val) {
Node nw = new Node(val);
if(index > size) return;
if(index == 0 || index<0){
size++;
}else if(index == size){
while(tmp.next != null) tmp = tmp.next;
tmp.next = nw;
nw.next = null;
size++;
}else{
for(int i = 0; i<=index; i++){
if(i == index-1){
nw.next = tmp.next;
tmp.next = nw;
size++;
}
tmp = tmp.next;
}
}
}

/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if(index <0) return;
int i = 0;
else{
while(i<index-1 && i<size){
tmp = tmp.next;
i++;
}
if(i < size && tmp.next != null){
if(tmp.next.next == null) tmp.next = null;
else{
tmp.next = tmp.next.next;
}
size--;
}
}
}
}

/**
* int param_1 = obj.get(index);
* obj.deleteAtIndex(index);
*/
```

Approach 2:

Java
```class MyLinkedList {
class Node{
int value;
Node next,prev;
Node(int val){
this.value = val;
}
}

int size;

/** Initialize your data structure here. */
size = 0;
}

/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
if(index < 0 || index >= size) return -1;
for(int i = 0; i<=size; i++){
if(i == index){
return tmp.value;
}
tmp = tmp.next;
}
return -1;
}

/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
Node nw = new Node(val);
nw.prev = null;
size++;
}

/** Append a node of value val to the last element of the linked list. */
Node nw = new Node(val);
for(int i = 0; i<size-1;i++) tmp = tmp.next;
tmp.next = nw;
nw.next = null;
nw.prev = tmp;
size++;
}

/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
public void addAtIndex(int index, int val) {
Node nw = new Node(val);
if(index > size) return;
if(index == 0 || index<0){
nw.prev = null;
size++;
}else if(index == size){
while(tmp.next != null) tmp = tmp.next;
tmp.next = nw;
nw.next = null;
nw.prev = tmp;
size++;
}else{
for(int i = 0; i<=index; i++){
if(i == index-1){
nw.next = tmp.next;
nw.prev = tmp;
tmp.next.prev = nw;
tmp.next = nw;
size++;
}
tmp = tmp.next;
}
}
}

/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if(index <0) return;
int i = 0;
else{
while(i<index-1 && i<size){
tmp = tmp.next;
i++;
}
if(i < size && tmp.next != null){
if(tmp.next.next == null) tmp.next = null; //check here
else{
tmp.next = tmp.next.next;
tmp.next.prev = tmp;
}
size--;
}
}
}
}

/**
* int param_1 = obj.get(index);
* obj.deleteAtIndex(index);
*/
```

Other Posts You May Like