Linked List Examples in C and Python

# Linked List Examples in C and Python

clickHere
Updated on Oct 11, 2023 18:14 IST

In the previous articles, you have gone through Linked List and their types. In this article, let us discuss the Top 25 operations of Linked Lists and their examples in C & Python.

## 1. Create a Linked List

### Create a Linked List with 4 nodes in C

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* i)
{
while (i != NULL) {
printf(" %d ", i->d);
i = i->n;
}
}
int main()
{
struct node* n_sec = NULL;
struct node* n_third = NULL;
struct node* n_fourth = NULL;

n_sec = (struct node*)malloc(sizeof(struct node));
n_third = (struct node*)malloc(sizeof(struct node));
n_fourth = (struct node*)malloc(sizeof(struct node));
n_sec->d = 23;
n_sec->n = n_third;
n_third->d = 37;
n_third->n = n_fourth;
n_fourth->d = 45;
n_fourth->n = NULL;
printf("Output :
");
return 0;
}```

### Create a Linked List with 4 nodes in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None
class CreateLL:
def __init__(self):

def DisplayList(self):
while (x):
print (x.d)
x = x.n

if __name__=='__main__':
c_ll = CreateLL()
n_sec = node(23)
n_third = node(37)
n_fourth = node(45)

n_sec.n = n_third;
n_third.n = n_fourth;
print('Output: ')
c_ll.DisplayList()```

## 2. Write a function to insert at the beginning of the list

### Insert at Beginning in C

```void InserBeg(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}```

### Insert at Beginning in Python

``` def InserBeg(self, new_d):
newnode = node(new_d)

## 3. Write a function to insert at the end of the list

### Insert at end in C

```void InserEnd(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
node1->n = NULL;

return;
}

while (end->n != NULL) end = end->n;

end->n = node1;
return;
}```

### Insert at end in Python

```def InserEnd(self, new_d):
newnode = node(new_d)

return

while (end.n):
end = end.n

end.n = newnode```

## 4. Write a function to insert after a node of the list

### Insert after a node in C

```void InserAf(struct node* p, int new_d) {
if (p == NULL) {
printf("the given previous node cannot be NULL");
return;
}

struct node* node1 = (struct node*)malloc(sizeof(struct node));
node1->d = new_d;
node1->n = p->n;
p->n = node1;
}```

### Insert after a node in Python

```def InserAf(self, p, new_d):

if p is None:
print("The given p node must in LinkedList.")
return

newnode = node(new_d)
newnode.n = p.n
p.n = newnode```

## 5. Find an element in Linked List

### Find an element in C

```int FindElement(struct node** head_ref, int key) {

while (current != NULL) {
if (current->d == key) return 1;
current = current->n;
}
return 0;
}
//Consider the Linked List - 62 31 10 19 12```

### Find an element in Python

```#Consider the Linked List - 62 31 10 19 12
def FindElement(self, key):
while current is not None:
if current.d == key:
return True
current = current.n

return False```

## 6. Write a function to delete a Linked List

### Function to Delete in C

```void DelNode(struct node** head_ref, int key) {
struct node *x = *head_ref, *prev;

if (x != NULL && x->d == key) {
free(x);
return;
}
while (x != NULL && x->d != key) {
prev = x;
x = x->n;
}
if (x == NULL) return;
prev->n = x->n;
free(x);
}```

### Function to Delete in Python

```# Deleting a node
def DelNode(self, index):
return
if index == 0:
x = None
return
for i in range(index - 1):
x = x.n
if x is None:
break
if x is None:
return
if x.n is None:
return
n = x.n.n
x.n = None
x.n = n```

## 7. Sort the Linked List in ascending order

### Sort in C

```//Consider the Linked List - 62 31 10 19 12
struct node *current = *head_ref, *index = NULL;
int temp;

return;
} else {
while (current != NULL) {
index = current->n;

while (index != NULL) {
if (current->d > index->d) {
temp = current->d;
current->d = index->d;
index->d = temp;
}
index = index->n;
}
current = current->n;
}
}
}```

### Sort in Python

```#Consider the Linked List - 62 31 10 19 12
index = node(None)

return
else:
while current is not None:
# index points to the node n to current
index = current.n

while index is not None:
if current.d > index.d:
current.d, index.d = index.d, current.d

index = index.n
current = current.n```

## 8. Find Length of a Linked List (Iterative and Recursive)

### Find Length in C

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}

// Insert
void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}
//Iteratively
{
int count = 0;
while (current != NULL)
{
count++;
current = current->n;
}
return count;
}
//Recursively
{
return 0;
}
else {
}
}

int main() {

");;
printf("
");
printf("Total number of elements in linked list are [Counted Interatively] : %d
printf("
");
printf("Total number of elements in linked list are [Counted Recursively] : %d
}```

### Find Length in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert at the beginning
def Insert(self, new_d):
newnode = node(new_d)

#Iteratively
def getCountIterative(self):
count = 0
while (temp):
count += 1
temp = temp.n
return count
#Recursive
def getCountRecursive(self, node):
if (not node): # Base case
return 0
else:
return 1 + self.getCountRecursive(node.n)

def getCount(self):

if __name__ == '__main__':
#Start the Linked List as empty elements
#Insert elements
llist.Insert(31)
llist.Insert(62)
llist.Insert(19)
llist.Insert(10)
llist.DisplayList()
print ('Total number of elements in linked list are [Counted Interatively] : ',llist.getCountIterative())
print()
print ('Total number of elements in linked list are [Counted Recursively] : ',llist.getCount())```

## 9. Write a function to get Nth node in a Linked List

### Go to Nth Node in C

```#include <stdio.h>
#include <stdlib.h>
struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}

// Insert
void Insert(struct node** headref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}
int goTo(struct node* head, int index)
{
int count = 0;
while (curr != NULL) {
if (count == index)
return (curr->d);
count++;
curr = curr->n;
}
}

int main() {

");;
printf("
");
printf("
The element at index 3 is -  %d", goTo(head, 3));
}```

### Go to Nth node in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert
def Insert(self, new_d):
newnode = node(new_d)

def goTo(self, index):
count = 0

# Loop while end of linked list is not reached
while (curr):
if (count == index):
return curr.d
count += 1
curr = curr.n

if __name__ == '__main__':
#Insert elements
llist.Insert(31)
llist.Insert(62)
llist.Insert(19)
llist.Insert(10)
llist.DisplayList()
i = 3
print()
print("Element at index 3 is :", llist.goTo(i))```

## 10. Nth node from the end of a Linked List

### Go to Nth Node from end in C

```#include<stdio.h>
#include<stdlib.h>
//structure of a node
struct node{
int d;
struct node *n;
}
int count = 0;
void DisplayList(){
printf("no node ");
else {
while(x!=NULL) {
printf("%d ",x->d);
x=x->n;
}
}
}
void Insert(int val){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->d = val;
newnode->n = NULL;
count++;
} else {
x->n=newnode;
x=x->n;
count++;
}
}
//Get the Nth node from end of list
void getN(int index){
int i;
for(i=0;i<count-index;i++){
x=x->n;
}
printf("
Print 3rd element from last : %d",x->d);
}
int main(){
int index=3;
Insert(10);
Insert(19);
Insert(62);
Insert(31);
printf("
Linked List after inserting elements : ");
DisplayList();
printf("
");
getN(index);
return 0;
}```

### Go to Nth node from end in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert
def Insert(self, new_d):
newnode = node(new_d)

def goTo(self, index):
count = 0

# Loop while end of linked list is not reached
while (curr):
if (count == index):
return curr.d
count += 1
curr = curr.n

if __name__ == '__main__':
#Insert elements
llist.Insert(31)
llist.Insert(62)
llist.Insert(19)
llist.Insert(10)
llist.DisplayList()
i = 3
print()
print("Element at index 3 is :", llist.goTo(i))```

## 11. Print the middle of a given linked list

### Print mid in C

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}

// Insert
void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}
{

{
while (y != NULL && y->n != NULL)
{
y = y->n->n;
x = x->n;
}
printf("
The middle element of the given Linked List is -
%d
", x->d);
}
}

int main() {

printf("
");;
printf("
");
}```

### Print the mid in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert at the beginning
def Insert(self, new_d):
newnode = node(new_d)

def getMid(self):
count = 0

while(x != None):
if count&1:
mid = mid.n
count += 1
x = x.n
if mid!=None:
print("The middle element of the Linked List is - ", mid.d)

if __name__ == '__main__':
#Start the Linked List as empty elements
#Insert elements
llist.Insert(31)
llist.Insert(62)
llist.Insert(19)
llist.Insert(10)
llist.Insert(34)
llist.DisplayList()
print()
llist.getMid()```

## 12. Write a function that counts the number of times a given integer occurs in a Linked List

### Function in C

```int getcount(struct node* head, int x)
{
int count = 0;
while (curr != NULL) {
if (curr->d == x)
count++;
curr = curr->n;
}
return count;
} ```

### Function in Python

``` def getcount(self, x):
count = 0
while(curr is not None):
if curr.d == x:
count += 1
curr = curr.n
return count```

## 13. Detect loop in a linked list

### Detect Loop in C

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}
// Insert
void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}
int getLoop(struct node* list)
{
struct node *x = list, *y = list;

while (x && y && y->n) {
x = x->n;
y = y->n->n;
if (x == y) {
return 1;
}
}
return 0;
}
int main() {

printf("
");;
printf("
");
printf("
Loop is Detected");
else
printf("
Loop is NOT Detected");
return 0;
}```

### Detect Loop in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert at the beginning
def Insert(self, new_d):
newnode = node(new_d)

def getLoop(self):
while(x and y and y.n):
x = x.n
y = y.n.n
if x == y:
return

if __name__ == '__main__':
#Start the Linked List as empty elements
#Insert elements
llist.Insert(31)
llist.Insert(62)
llist.Insert(19)
llist.Insert(10)
llist.DisplayList()
print()
if(llist.getLoop()):
print ("Loop is Detected")
else:
print ("Loop is NOT Detected")```

## 14. Function to check if a singly linked list is a palindrome

### Check palindrome in C

```bool getPalWrap(struct node** l, struct node* r)
{
if (r == NULL)
return true;
bool x = getPalWrap(l, r->n);
if (x == false)
return false;
bool x1 = (r->d == (*l)->d);
*l = (*l)->n;
return x1;
}

{
}```

### Check palindrome in Python

```def getPalUtil(r):
if (r == None):
return True
x = getPalUtil(r.next)
if (x == False):
return False
x1 = (r.d == l.d)
l = l.next
return x1
return result```

### Output:

Want to explore more about palindrome, check our blog How to Check if a Python String is a Palindrome?

## 15. Remove duplicates from a sorted linked list

### Remove in C

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}
// Insert
void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}

{
struct node* n_n;
if (current == NULL)
return;
while (current->n != NULL)
{
if (current->d == current->n->d)
{
n_n = current->n->n;
free(current->n);
current->n = n_n;
}
else
{
current = current->n;
}
}
}

int main() {

printf("
");;
printf("
");
printf("
Linked list after deleting duplicate elements :
");
}
```

### Remove in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert at the beginning
def Insert(self, new_d):
newnode = node(new_d)

def DelNode(self, index):
return
if index == 0:
x = None
return
for i in range(index - 1):
x = x.n
if x is None:
break
if x is None:
return
if x.n is None:
return
n = x.n.n
x.n = None
x.n = n
def DelDup(self):
if temp is None:
return
while temp.n is not None:
if temp.d == temp.n.d:
new = temp.n.n
temp.n = None
temp.n = new
else:
temp = temp.n

if __name__ == '__main__':
#Start the Linked List as empty elements
#Insert elements
llist.Insert(10)
llist.Insert(19)
llist.Insert(31)
llist.Insert(31)
llist.Insert(62)
llist.DisplayList()
print('Linked List after deleting the duplicate elements :')
llist.DelDup()
llist.DisplayList()```

## 16. Remove duplicates from an unsorted linked list

### Remove in C

```#include<stdio.h>
#include<stdlib.h>
//structure of a node
struct node{
int d;
struct node *n;
}
int count = 0;
void DisplayList(){
printf("no node ");
else {
while(x!=NULL) {
printf("%d ",x->d);
x=x->n;
}
}
}
void Insert(int val){
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->d = val;
newnode->n = NULL;
count++;
} else {
x->n=newnode;
x=x->n;
count++;
}
}
void DelDup() {
struct node *curr = head, *key = NULL, *x = NULL;
return;
}
else {
while(curr != NULL){
x = curr;
key = curr->n;
while(key != NULL) {
if(curr->d == key->d) {
x->n = key->n;
}
else {
x = key;
}
key = key->n;
}
curr = curr->n;
}
}
}

int main(){
int key=3;
Insert(62);
Insert(31);
Insert(31);
Insert(75);
Insert(21);
printf("
Linked List after inserting elements : ");
DisplayList();
printf("
");
printf("
Linked List after deleting elements : ");
DelDup();
DisplayList();
return 0;
}```

### Remove in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (x):
print(x.d)
x = x.n

# Insert at the beginning
def Insert(self, new_d):
newnode = node(new_d)

def DelNode(self, key):
return
if key == 0:
x = None
return
for i in range(key - 1):
x = x.n
if x is None:
break
if x is None:
return
if x.n is None:
return
n = x.n.n
x.n = None
x.n = n

def DelDup(self):
key = None;
x = None;
return;
else:
while(curr != None):
x = curr;
key = curr.n;
while(key != None):
if(curr.d == key.d):
x.n = key.n;
else:
x = key;
key = key.n;
curr = curr.n;

if __name__ == '__main__':
#Insert elements
llist.Insert(21)
llist.Insert(75)
llist.Insert(31)
llist.Insert(31)
llist.Insert(62)
llist.DisplayList()
print()
print('Linked List after deleting the duplicate elements :')
llist.DelDup()
llist.DisplayList()```

## 17. Swap nodes in a linked list without swapping data

### Swap nodes in C

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

struct node {
int d;
struct node* n;
};

void DisplabList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}
// Insert
void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}
void getSwap(struct node** head_ref, int a, int b)
{
if (a == b)
return;
struct node *p1 = NULL, *curr1 = *head_ref;
while (curr1 && curr1->d != a) {
p1 = curr1;
curr1 = curr1->n;
}
struct node *p2 = NULL, *curr2 = *head_ref;
while (curr2 && curr2->d != b) {
p2 = curr2;
curr2 = curr2->n;
}
if (curr1 == NULL || curr1 == NULL)
return;
if (p1 != NULL)
p1->n = curr2;
else
if (p2 != NULL)
p2->n = curr1;
else
struct node* swap = curr2->n;
curr2->n = curr1->n;
curr1->n = swap;
}
int main() {

printf("
");;
printf("
");
printf("
Linked list after swapping nodes :
");
}
```

### Swap nodes in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert at the beginning
def Insert(self, new_d):
newnode = node(new_d)

def getSwap(self, x, y):
if x == y:
return
p1 = None
while curr1 != None and curr1.d != x:
p1 = curr1
curr1 = curr1.n
p2 = None
while curr2 != None and curr2.d != y:
p2 = curr2
curr2 = curr2.n
if curr1 == None or curr2 == None:
return
if p1 != None:
p1.n = curr2
else:
if p2 != None:
p2.n = curr1
else:
temp = curr1.n
curr1.n = curr2.n
curr2.n = temp

if __name__ == '__main__':
#Start the Linked List as empty elements
#Insert elements
llist.Insert(31)
llist.Insert(62)
llist.Insert(19)
llist.Insert(10)
llist.DisplayList()
print()
llist.getSwap(19, 62)
print()
print ('Linked list after swapping nodes ')
llist.DisplayList()```

## 18. Pairwise swap elements of a given linked list

### Swap in C

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}
// Insert
void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}
{
while (temp != NULL && temp->n != NULL) {
swap(&temp->d, &temp->n->d);
temp = temp->n->n;
}
}

void swap(int* x, int* y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

int main() {

printf("
");;
printf("
");
printf("
Linked list after swapping elements in pair:
");

}```

### Swap in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert at the beginning
def Insert(self, new_d):
newnode = node(new_d)

def getpairSwap(self):
if temp is None:
return
while(temp and temp.n):
if(temp.d != temp.n.d):
temp.d, temp.n.d = temp.n.d, temp.d
temp = temp.n.n
if __name__ == '__main__':
#Start the Linked List as empty elements
#Insert elements
llist.Insert(31)
llist.Insert(62)
llist.Insert(19)
llist.Insert(10)
llist.DisplayList()
print()
llist.getpairSwap()
print()
print ('Linked list after swapping elements in pair ')
llist.DisplayList()```

## 19. Move the last element to the front of a given Linked List

### Move in C

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}
// Insert
void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}
{
return;
struct node *secondend = NULL;
while (end->n != NULL)
{
secondend = end;
end = end->n;
}
secondend->n = NULL;
}

int main() {

printf("
");;
printf("
");
printf("
Linked list after moving the last element to 1st position:
");

}```

### Move in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert at the beginning
def Insert(self, new_d):
newnode = node(new_d)

def getMove(self):
secondend = None
if not temp or not temp.n:
return
while temp and temp.n :
secondend = temp
temp = temp.n
secondend.n = None
if __name__ == '__main__':
#Start the Linked List as empty elements
#Insert elements
llist.Insert(31)
llist.Insert(62)
llist.Insert(19)
llist.Insert(10)
llist.DisplayList()
print()
llist.getMove()
print()
print ('Linked list after moving the end element to 1st position: ')
llist.DisplayList()
```

## 20. Identify if 2 lists are identical or not

### Identify in C

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}

void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}

bool getIdentical(struct node *x, struct node *y)
{
while (x != NULL && y != NULL)
{
if (x->d != y->d)
return false;
x = x->n;
y = y->n;
}
return (x == NULL && y == NULL);
}

int main() {

printf("
Elements in Linked List - 1 :
");;
printf("
");
printf("
Elements in Linked List - 2 :
");;
Both the Lists are identical"):
printf("
Both the lists are NOT identical");
return 0;
}```

### Identify in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert at the beginning
def Insert(self, new_d):
newnode = node(new_d)

def getIdentical(self, list2):
while (x != None and y != None):
if (x.d != y.d):
return False
x = x.n
y = y.n
return (a == None and b == None)

if __name__ == '__main__':
#Start the Linked List as empty elements
#Insert elements
llist1.Insert(31)
llist1.Insert(62)
llist1.Insert(19)
llist1.Insert(10)
print('Elements in Linked List - 1')
llist1.DisplayList()
print()
llist2.Insert(11)
llist2.Insert(62)
llist2.Insert(19)
llist2.Insert(10)
print('Elements in Linked List - 2')
llist2.DisplayList()
print()
if (llist1.getIdentical(llist2) == True):
print("Both the lists are identical ")
else:
print("Both the lists are NOT identical ")```

## 21. Create and insert elements in Doubly Linked List

### Create in C

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

struct node {
int d;
struct node* n;
struct node* p;
};

//Insert at Beginning
void InserBeg(struct node** head, int d) {
struct node* newnode = (struct node*)malloc(sizeof(struct node));

newnode->d = d;
newnode->p = NULL;
}

// Insert after a node
void InserAf(struct node* p_node, int d) {
if (p_node == NULL) {
printf("The previous node must not be NULL");
return;
}
struct node* newnode = (struct node*)malloc(sizeof(struct node));

newnode->d = d;
newnode->n = p_node->n;
p_node->n = newnode;
newnode->p = p_node;
if (newnode->n != NULL)
newnode->n->p = newnode;
}

// Insert at the end
void InserEnd(struct node** head, int d) {
struct node* newnode = (struct node*)malloc(sizeof(struct node));

newnode->d = d;

// assign null to n of newnode
newnode->n = NULL;
newnode->p = NULL;
return;
}
while (temp->n != NULL)
temp = temp->n;
temp->n = newnode;
newnode->p = temp;
}

// print the doubly linked list
void DisplayList(struct node* node) {
struct node* last;

while (node != NULL) {
printf("%d ", node->d);
last = node;
node = node->n;
}
if (node == NULL)
printf("NULL
");
}

int main() {
//Insert elements

printf("Linked List after inserting elements :
");
}```

### Create in Python

```import gc

class Node:

def __init__(self, d):
self.d = d
self.n = None
self.p = None

class DoublyLL:

def __init__(self):

# print the doubly linked list
def DisplayList(self, node):

while node:
print(node.d, end="  ")
last = node
node = node.n

# insert node at the front
def InserBeg(self, d):

node1 = Node(d)

# insert a node after a particular node
def InserAf(self, p_node, d):

if p_node is None:
print("pious node cannot be null")
return

node1 = Node(d)
node1.n = p_node.n
p_node.n = node1
node1.p = p_node

if node1.n:
node1.n.p = node1

# insert a newNode at the end of the list
def InserEnd(self, d):

node1 = Node(d)
return

while temp.n:
temp = temp.n
temp.n = node1
node1.p = temp

return

# free the memory
gc.collect()

# initialize an empty node
D_ll = DoublyLL()
#Insert elements
D_ll.InserEnd(42)
D_ll.InserBeg(31)
D_ll.InserBeg(62)
D_ll.InserEnd(12)
D_ll.InserEnd(85)

print('Linked List after inserting elements :')

print()```

### Output

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}

void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}

struct node* newnode(int d)
{
struct node* node1
= (struct node*)malloc(sizeof(struct node));
node1->d = d;
node1->n = NULL;
return node1;
}

{
struct node* final = NULL;
struct node *temp, *p = NULL;
carry = (add >= 10) ? 1 : 0;
if (final == NULL)
final = temp;
else
p->n = temp;
p = temp;
}

if (carry > 0)
temp->n = newnode(carry);
return final;
}

int main() {
struct node* final = NULL;

printf("
Elements in Linked List - 1 :
");;
printf("
");
printf("
Elements in Linked List - 2 :
");;
printf("
");
printf("
After adding the numbers present in the list : ");
DisplayList(final);
}```

### Output

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert at the beginning
def Insert(self, new_d):
newnode = node(new_d)

p = None
temp = None
carry = 0

while(llist1 is not None or llist2 is not None):
x = 0 if llist1 is None else llist1.d
y = 0 if llist2 is None else llist2.d
add = carry + x + y
carry = 1 if add >= 10 else 0
else:
p.n = temp
p = temp
if llist1 is not None:
llist1 = llist1.n
if llist2 is not None:
llist2 = llist2.n
if carry > 0:
temp.n = node(carry)

if __name__ == '__main__':
#Insert elements
llist1.Insert(4)
llist1.Insert(6)
llist1.Insert(1)
llist1.Insert(0)
print('Elements in Linked List - 1')
llist1.DisplayList()
print()
llist2.Insert(3)
llist2.Insert(2)
llist2.Insert(6)
llist2.Insert(1)
print('Elements in Linked List - 2')
llist2.DisplayList()
print ("
After adding the numbers present in the list : ",end=' ')
print()
final.DisplayList()```

## 23. Create and insert elements in Circular Linked List

### Create in C

```//Circular Singly Linked List in C
#include <stdio.h>
#include <stdlib.h>
struct node {
int d;
struct node* n;
};

struct node* EmptyList(struct node* end, int d) {
if (end != NULL) return end;

struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->d = d;
end = newnode;
end->n = end;
return end;
}
//Traverse
void DisplayList(struct node* end) {
struct node* p;
if (end == NULL) {
printf("Sorry! Empty List");
return;
}
p = end->n;
do {
printf("%d ", p->d);
p = p->n;
} while (p != end->n);
}

// Insert at Beginning
struct node* InserBeg(struct node* end, int d) {
if (end == NULL) return EmptyList(end, d);
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->d = d;
newnode->n = end->n;
end->n = newnode;
return end;
}

// Insert at End
struct node* InserEnd(struct node* end, int d) {
if (end == NULL) return EmptyList(end, d);
struct node* newnode = (struct node*)malloc(sizeof(struct node));
newnode->d = d;
newnode->n = end->n;
end->n = newnode;
end = newnode;
return end;
}

// Insert after a node
struct node* InserAf(struct node* end, int d, int item) {
if (end == NULL) return NULL;
struct node *newnode, *p;
p = end->n;
do {
if (p->d == item) {
newnode = (struct node*)malloc(sizeof(struct node));
newnode->d = d;
newnode->n = p->n;
p->n = newnode;
if (p == end) end = newnode;
return end;
}

p = p->n;
} while (p != end->n);

printf("
Sorry! Node not present in the list");
return end;
}

int main() {
struct node* end = NULL;

end = EmptyList(end, 42);
end = InserEnd(end, 31);
end = InserBeg(end, 62);
end = InserBeg(end, 12);
end = InserEnd(end, 85);
end = InserAf(end, 10, 42);
printf("Linked List after inserting elements :
");
DisplayList(end);
printf("
");
return 0;
}```

### Create in Python

```import gc
class node:
def __init__(self, d):
self.d = d
self.n = None

class CircularLL:
def __init__(self):

#Traverse & Display
def DisplayList(self):
if(x != None):
print()
print("Final Circular Linked List : ", end="  ")
print()
while (True):
print(x.d, end=" ")
x = x.n
break
print()
else:
print("The list is empty.")

#Insert at beginning
def InserBeg(self, ele):
newnode = node(ele)
return
else:
x = x.n
x.n = newnode

#Insert at end
def InserEnd(self, ele):
newnode = node(ele)
return
else:
x = x.n
x.n = newnode

#Inserts a new element at the given pos
def InserAf(self, ele, pos):
newnode = node(ele)
count = 0
if(x != None):
count += 1
x = x.n
count += 1
x = x.n

if(pos < 1 or pos > (count+1)):
print("
Inavalid pos.")
elif (pos == 1):
else:
x = x.n
else:
for i in range(1, pos-1):
x = x.n
newnode.n = x.n
x.n = newnode

C_ll = CircularLL()

#Insert at Beginning
C_ll.InserBeg(72)
C_ll.InserBeg(21)
C_ll.InserBeg(99)
C_ll.DisplayList()

#Insert at End
C_ll.InserEnd(11)
C_ll.InserEnd(22)
C_ll.InserEnd(32)
C_ll.InserEnd(45)
C_ll.DisplayList()

#Insert after an element
C_ll.InserAf(59, 2)
C_ll.DisplayList()
```

## 24. Segregate even and odd nodes in a Linked List

### Segregate in C

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}

void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}
{
struct node *p = NULL;
while (last->n != NULL)
last = last->n;

struct node *new_last = last;
while (curr->d %2 != 0 && curr != last)
{
new_last->n = curr;
curr = curr->n;
new_last->n->n = NULL;
new_last = new_last->n;
}

if (curr->d%2 == 0)
{
while (curr != last)
{
if ( (curr->d)%2 == 0 )
{
p = curr;
curr = curr->n;
}
else
{
p->n = curr->n;
curr->n = NULL;
new_last->n = curr;
new_last = curr;
curr = p->n;
}
}
}
else p = curr;
if (new_last!=last && (last->d)%2 != 0)
{
p->n = last->n;
last->n = NULL;
new_last->n = last;
}
return;
}

int main() {

");;
printf("
");
printf("
After segregating even and odd nodes :
");
}```

### Segregate in Python

```head = None

class node:
def __init__(self, d):
self.d = d
self.n =None

def getSeg():
p = None
while (end.n != None):
end = end.n
new_end = end
while (curr.d % 2 !=0 and curr != end):
new_end.n = curr
curr = curr.n
new_end.n.n = None
new_end = new_end.n
if (curr.d % 2 == 0):
while (curr != end):
if (curr.d % 2 == 0):
p = curr
curr = curr.n
else:
p.n = curr.n
curr.n = None
new_end.n = curr
new_end = curr
curr = p.n
else:
p = curr
if (new_end != end and end.d % 2 != 0):
p.n = end.n
end.n = None
new_end.n = end

def Insert(new_d):
node1 = node(new_d)

def DisplayList():
while(temp != None):
print(temp.d, end = " ")
temp = temp.n

print(" ")

Insert(62)
Insert(31)
Insert(19)
Insert(10)
print("Linked List after inserting elements : ")
DisplayList()
print()
getSeg()
print("Linked List after segregation  : ")
DisplayList()```

### Reverse in C

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

struct node {
int d;
struct node* n;
};

void DisplayList(struct node* node) {
while (node != NULL) {
printf(" %d ", node->d);
node = node->n;
}
}

void Insert(struct node** head_ref, int new_d) {
struct node* node1 = (struct node*)malloc(sizeof(struct node));

node1->d = new_d;
}
{
struct node* p = NULL;
struct node* n = NULL;
while (curr != NULL) {
n = curr->n;
curr->n = p;
p = curr;
curr = n;
}
}

int main() {

");;
printf("
");
printf("
");
}```

### Reverse in Python

```class node:
def __init__(self, d):
self.d = d
self.n = None

def __init__(self):

def DisplayList(self):
while (temp):
print(temp.d)
temp = temp.n

# Insert
def Insert(self, new_d):
newnode = node(new_d)

def getRevList(self):
p = None
while(curr is not None):
n = curr.n
curr.n = p
p = curr
curr = n

if __name__ == '__main__':
#Insert elements
llist.Insert(31)
llist.Insert(62)
llist.Insert(19)
llist.Insert(10)
print('Linked List after inserting elements :')
llist.DisplayList()
llist.getRevList()
print ("
llist.DisplayList()```

### Output

Top Trending Tech Articles:
Career Opportunities after BTech | Online Python Compiler | What is Coding | Queue Data Structure | Top Programming Language | Trending DevOps Tools | Highest Paid IT Jobs | Most In Demand IT Skills | Networking Interview Questions | Features of Java | Basic Linux Commands | Amazon Interview Questions

Recently completed any professional course/certification from the market? Tell us what liked or disliked in the course for more curated content.