Linked List
Published: 2022-07-21 14:57:00

**Inserting into linked list at any position**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/asyx2myw9uoumd2mkwa7.png)

```
#include <stdio.h>
#include <stdlib.h>

struct Node
{
  int data;
  struct Node *next;
};
struct Node *head;
void print()
{
  Node *temp = head;
  while (temp != NULL)
  {
    printf("%d ", temp->data);
    temp = temp->next;
  }
  printf("\n");
}

void insert(int data, int n)
{
  Node *temp1 = new Node();
  temp1->data = data;
  temp1->next = NULL;
  if (n == 1)
  {
    temp1->next = head;
    head = temp1;
    print();
    return;
  }
  Node *temp2 = head;
  for (int i = 0; i < n - 2; i++)
  {
    temp2 = temp2->next;
  }

  temp1->next = temp2->next;
  temp2->next = temp1;
  print();
}

int main()
{
  head = NULL; // empty list
  insert(2, 1);
  insert(3, 2);
  insert(4, 1);
  insert(5, 2);
  return 0;
}
```
**Deleting the node in the linked list**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/16utud2oatw4x0cufzar.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5dahmujfta2tj86crs24.png)
```
#include <stdio.h>
#include <stdlib.h>

struct Node
{
  int data;
  struct Node *next;
};
struct Node *head;
void printList()
{
  Node *temp = head;
  while (temp != NULL)
  {
    printf("[%d|%p]->", temp->data, (void *)&temp);
    temp = temp->next;
  }
  printf("\n");
}
void insert(int data)
{
  Node *temp = new Node();
  temp->data = data;
  temp->next = head;
  head = temp;
}
void deleteList(int n)
{
  struct Node *temp1 = head;
  if (n == 1)
  {
    head = temp1->next;
    free(temp1);
    return;
  }
  int i;
  for (i = 0; i < n - 2; i++)
  {
    temp1 = temp1->next;
  }
  printf("deleted data: %d ", temp1->next->data);

  printf("\n");
  struct Node *temp2 = temp1->next;
  temp1->next = temp2->next;
  free(temp2);
}
int main()
{
  head = NULL;
  int n;
  insert(1);
  insert(2);
  insert(3);
  insert(4);
  insert(5);
  insert(6);
  insert(7);
  printList();
  printf("Enter a position to delete \n");
  scanf("%d", &n);
  deleteList(n);
  printList();
  return 0;
}
```

**Reversing a linked list**

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j2wskesfqz4sivqpcu9b.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dc31n3bk1cppseuinh1v.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6mkh777nypo3a33ojwv0.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/31pl7aa251iusukeonj5.png)
```
#include <stdio.h>
#include <stdlib.h>
struct Node
{
  int data;
  struct Node *next;
};
struct Node *head;
void reverseList()
{
  struct Node *prev, *current, *next;
  current = head;
  prev = NULL;
  while (current != NULL)
  {
    next = current->next;
    current->next = prev; // linking to prev node
    // updating for the next node
    prev = current;
    current = next;
  }
  head = prev;
}
void printList()
{
  Node *temp = head;
  while (temp != NULL)
  {
    printf("%d ", temp->data);
    temp = temp->next;
  }
  printf("\n");
}
void insertNode(int data)
{
  Node *temp = new Node();
  temp->data = data;
  temp->next = head;
  head = temp;
}
int main()
{
  struct Node *head = NULL;
  insertNode(1);
  insertNode(2);
  insertNode(3);
  insertNode(4);
  printf("Linked List : ");
  printList();
  reverseList();
  printf("Reversed list: ");
  printList();
  return 0;
}
```