Linked List Examples in C and Python
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.
Following are the examples covered in this article:
- Create a Linked List
- Write a function to insert at the beginning of the list
- Write a function to insert at the end of the list
- Write a function to insert after a node of the list
- How to find an element in the list
- Write a function to delete after a node in Linked List
- Sort the Linked List in ascending order
- Find Length of a Linked List (Iterative and Recursive)
- Write a function to get Nth node in a linked list
- Go to the Nth node from the end of a linked list
- How to print the midpoint of a linked list
- Write a function to count the number of times a given integer occurs in a Linked List
- How to detect loop in a linked list
- Write a function to check if a singly linked list is a palindrome
- How to remove duplicates from a sorted linked list
- How to remove duplicates from an unsorted linked list
- Swap nodes in a linked list without swapping data
- Swap elements of a linked list in pairs
- Shift the last element to the beginning of a Linked List
- Identify if two lists are identical or not
- Create and insert elements in Doubly Linked List
- How to add elements of 2 Linked Lists
- Create and insert elements in Circular Linked List
- How to segregate even and odd nodes in a Linked List
- How to reverse a linked list
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* head = NULL; struct node* n_sec = NULL; struct node* n_third = NULL; struct node* n_fourth = NULL; head = (struct node*)malloc(sizeof(struct node)); n_sec = (struct node*)malloc(sizeof(struct node)); n_third = (struct node*)malloc(sizeof(struct node)); n_fourth = (struct node*)malloc(sizeof(struct node)); head->d = 14; head->n = n_sec; 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 : "); DisplayList(head); return 0; }
Output
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): self.head = None def DisplayList(self): x = self.head while (x): print (x.d) x = x.n if __name__=='__main__': c_ll = CreateLL() c_ll.head = node(14) n_sec = node(23) n_third = node(37) n_fourth = node(45) c_ll.head.n = n_sec; n_sec.n = n_third; n_third.n = n_fourth; print('Output: ') c_ll.DisplayList()
Best-suited Data Structures and Algorithms courses for you
Learn Data Structures and Algorithms with these high-rated online courses
Output
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; node1->n = (*head_ref); (*head_ref) = node1; }
Output
Insert at Beginning in Python
def InserBeg(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode
Output
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)); struct node* end = *head_ref; node1->d = new_d; node1->n = NULL; if (*head_ref == NULL) { *head_ref = node1; return; } while (end->n != NULL) end = end->n; end->n = node1; return; }
Output
Insert at end in Python
def InserEnd(self, new_d): newnode = node(new_d) if self.head is None: self.head = newnode return end = self.head while (end.n): end = end.n end.n = newnode
Output
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; }
Output
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
Output
5. Find an element in Linked List
Find an element in C
int FindElement(struct node** head_ref, int key) { struct node* current = *head_ref; while (current != NULL) { if (current->d == key) return 1; current = current->n; } return 0; } //Consider the Linked List - 62 31 10 19 12
Output
Find an element in Python
#Consider the Linked List - 62 31 10 19 12 def FindElement(self, key): current = self.head while current is not None: if current.d == key: return True current = current.n return False
Output
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) { *head_ref = x->n; free(x); return; } while (x != NULL && x->d != key) { prev = x; x = x->n; } if (x == NULL) return; prev->n = x->n; free(x); }
Output:
Function to Delete in Python
# Deleting a node def DelNode(self, index): if self.head is None: return x = self.head if index == 0: self.head = x.n 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
Output
7. Sort the Linked List in ascending order
Sort in C
//Consider the Linked List - 62 31 10 19 12 void SortLL(struct node** head_ref) { struct node *current = *head_ref, *index = NULL; int temp; if (head_ref == NULL) { 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; } } }
Output
Sort in Python
#Consider the Linked List - 62 31 10 19 12 def SortLL(self, head): current = head index = node(None) if head is 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
Output
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; node1->n = (*head_ref); (*head_ref) = node1; } //Iteratively int getCountIterative(struct node* head) { int count = 0; struct node* current = head; while (current != NULL) { count++; current = current->n; } return count; } //Recursively int getCountRecursive(struct node* head) { if (head == NULL) { return 0; } else { return 1 + getCountRecursive(head->n); } } int main() { struct node* head = NULL; Insert(&head, 31); Insert(&head, 62); Insert(&head, 19); Insert(&head, 10); printf("Elements in Linked List: ");; DisplayList(head); printf(" "); printf("Total number of elements in linked list are [Counted Interatively] : %d ", getCountIterative(head)); printf(" "); printf("Total number of elements in linked list are [Counted Recursively] : %d ", getCountRecursive(head)); }
Output
Find Length in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert at the beginning def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode #Iteratively def getCountIterative(self): temp = self.head 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): return self.getCountRecursive(self.head) if __name__ == '__main__': #Start the Linked List as empty elements llist = LinkedList() #Insert elements llist.Insert(31) llist.Insert(62) llist.Insert(19) llist.Insert(10) print('Linked List after inserting elements:') 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())
Output
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; node1->n = (*headref); (*headref) = node1; } int goTo(struct node* head, int index) { struct node* curr = head; int count = 0; while (curr != NULL) { if (count == index) return (curr->d); count++; curr = curr->n; } } int main() { struct node* head = NULL; Insert(&head, 31); Insert(&head, 62); Insert(&head, 19); Insert(&head, 10); printf("Elements in Linked List: ");; DisplayList(head); printf(" "); printf(" The element at index 3 is - %d", goTo(head, 3)); }
Output
Go to Nth node in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def goTo(self, index): curr = self.head 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__': llist = LinkedList() #Insert elements llist.Insert(31) llist.Insert(62) llist.Insert(19) llist.Insert(10) print('Linked List after inserting elements:') llist.DisplayList() i = 3 print() print("Element at index 3 is :", llist.goTo(i))
Output
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; } *head,*x; int count = 0; void DisplayList(){ if(head==NULL) printf("no node "); else { x=head; 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; if(head == NULL){ head = newnode; x = head; count++; } else { x->n=newnode; x=x->n; count++; } } //Get the Nth node from end of list void getN(int index){ int i; x=head; for(i=0;i<count-index;i++){ x=x->n; } printf(" Print 3rd element from last : %d",x->d); } int main(){ struct node* head = NULL; int index=3; Insert(10); Insert(19); Insert(62); Insert(31); printf(" Linked List after inserting elements : "); DisplayList(); printf(" "); getN(index); return 0; }
Output
Go to Nth node from end in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def goTo(self, index): curr = self.head 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__': llist = LinkedList() #Insert elements llist.Insert(31) llist.Insert(62) llist.Insert(19) llist.Insert(10) print('Linked List after inserting elements:') llist.DisplayList() i = 3 print() print("Element at index 3 is :", llist.goTo(i))
Output
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; node1->n = (*head_ref); (*head_ref) = node1; } void getMid(struct node *head) { struct node *x = head; struct node *y = head; if (head!=NULL) { 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() { struct node* head = NULL; Insert(&head, 31); Insert(&head, 62); Insert(&head, 19); Insert(&head, 10); Insert(&head, 34); printf(" Elements in Linked List : ");; DisplayList(head); printf(" "); getMid(head); }
Output
Print the mid in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert at the beginning def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def getMid(self): count = 0 mid = self.head x = self.head 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 llist = LinkedList() #Insert elements llist.Insert(31) llist.Insert(62) llist.Insert(19) llist.Insert(10) llist.Insert(34) print('Linked List after inserting elements:') llist.DisplayList() print() llist.getMid()
Output
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) { struct node* curr = head; int count = 0; while (curr != NULL) { if (curr->d == x) count++; curr = curr->n; } return count; }
Output:
Function in Python
def getcount(self, x): curr = self.head count = 0 while(curr is not None): if curr.d == x: count += 1 curr = curr.n return count
Output:
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; node1->n = (*head_ref); (*head_ref) = node1; } 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() { struct node* head = NULL; Insert(&head, 31); Insert(&head, 62); Insert(&head, 19); Insert(&head, 10); printf(" Elements in Linked List : ");; DisplayList(head); printf(" "); head->n->n->n = head; if (getLoop(head)) printf(" Loop is Detected"); else printf(" Loop is NOT Detected"); return 0; }
Output:
Detect Loop in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert at the beginning def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def getLoop(self): x = self.head y = self.head 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 llist = LinkedList() #Insert elements llist.Insert(31) llist.Insert(62) llist.Insert(19) llist.Insert(10) print('Linked List after inserting elements:') llist.DisplayList() print() if(llist.getLoop()): print ("Loop is Detected") else: print ("Loop is NOT Detected")
Output
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; } bool getPal(struct node* head) { getPalWrap(&head, head); }
Output:
Check palindrome in Python
def getPalUtil(r): global head, l l = head if (r == None): return True x = getPalUtil(r.next) if (x == False): return False x1 = (r.d == l.d) l = l.next return x1 def getPal(head): result = getPalUtil(head) 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; node1->n = (*head_ref); (*head_ref) = node1; } void DelDup(struct node* head) { struct node* current = head; 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() { struct node* head = NULL; Insert(&head, 10); Insert(&head, 19); Insert(&head, 31); Insert(&head, 31); Insert(&head, 62); printf(" Elements in Linked List : ");; DisplayList(head); printf(" "); DelDup(head); printf(" Linked list after deleting duplicate elements : "); DisplayList(head); }
Output:
Remove in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert at the beginning def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def DelNode(self, index): if self.head is None: return x = self.head if index == 0: self.head = x.n 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): temp = self.head 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 return self.head if __name__ == '__main__': #Start the Linked List as empty elements llist = LinkedList() #Insert elements llist.Insert(10) llist.Insert(19) llist.Insert(31) llist.Insert(31) llist.Insert(62) print('Linked List after inserting elements:') llist.DisplayList() print('Linked List after deleting the duplicate elements :') llist.DelDup() llist.DisplayList()
Output:
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; } *head,*x; int count = 0; void DisplayList(){ if(head==NULL) printf("no node "); else { x=head; 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; if(head == NULL){ head = newnode; x = head; count++; } else { x->n=newnode; x=x->n; count++; } } void DelDup() { struct node *curr = head, *key = NULL, *x = NULL; if(head == 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(){ struct node* head = NULL; 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; }
Output:
Remove in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): x = self.head while (x): print(x.d) x = x.n # Insert at the beginning def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def DelNode(self, key): if self.head is None: return x = self.head if key == 0: self.head = x.n 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): curr = self.head; key = None; x = None; if(self.head == 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__': llist = LinkedList() #Insert elements llist.Insert(21) llist.Insert(75) llist.Insert(31) llist.Insert(31) llist.Insert(62) print('Linked List after inserting elements:') llist.DisplayList() print() print('Linked List after deleting the duplicate elements :') llist.DelDup() llist.DisplayList()
Output:
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; node1->n = (*head_ref); (*head_ref) = node1; } 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 *head_ref = curr2; if (p2 != NULL) p2->n = curr1; else *head_ref = curr1; struct node* swap = curr2->n; curr2->n = curr1->n; curr1->n = swap; } int main() { struct node* head = NULL; Insert(&head, 31); Insert(&head, 62); Insert(&head, 19); Insert(&head, 10); printf(" Elements in Linked List : ");; DisplabList(head); printf(" "); getSwap(&head, 62, 19); printf(" Linked list after swapping nodes : "); DisplabList(head); }
Output:
Swap nodes in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert at the beginning def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def getSwap(self, x, y): if x == y: return p1 = None curr1 = self.head while curr1 != None and curr1.d != x: p1 = curr1 curr1 = curr1.n p2 = None curr2 = self.head 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: self.head = curr2 if p2 != None: p2.n = curr1 else: self.head = curr1 temp = curr1.n curr1.n = curr2.n curr2.n = temp if __name__ == '__main__': #Start the Linked List as empty elements llist = LinkedList() #Insert elements llist.Insert(31) llist.Insert(62) llist.Insert(19) llist.Insert(10) print('Linked List after inserting elements:') llist.DisplayList() print() llist.getSwap(19, 62) print() print ('Linked list after swapping nodes ') llist.DisplayList()
Output:
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; node1->n = (*head_ref); (*head_ref) = node1; } void getpairSwap(struct node* head) { struct node* temp = head; 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() { struct node* head = NULL; Insert(&head, 31); Insert(&head, 62); Insert(&head, 19); Insert(&head, 10); printf(" Elements in Linked List : ");; DisplayList(head); printf(" "); getpairSwap(head); printf(" Linked list after swapping elements in pair: "); DisplayList(head); }
Output
Swap in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert at the beginning def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def getpairSwap(self): temp = self.head 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 llist = LinkedList() #Insert elements llist.Insert(31) llist.Insert(62) llist.Insert(19) llist.Insert(10) print('Linked List after inserting elements:') llist.DisplayList() print() llist.getpairSwap() print() print ('Linked list after swapping elements in pair ') llist.DisplayList()
Output
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; node1->n = (*head_ref); (*head_ref) = node1; } void getMove(struct node **head_ref) { if (*head_ref == NULL || (*head_ref)->n == NULL) return; struct node *secondend = NULL; struct node *end = *head_ref; while (end->n != NULL) { secondend = end; end = end->n; } secondend->n = NULL; end->n = *head_ref; *head_ref = end; } int main() { struct node* head = NULL; Insert(&head, 31); Insert(&head, 62); Insert(&head, 19); Insert(&head, 10); printf(" Elements in Linked List : ");; DisplayList(head); printf(" "); getMove(&head); printf(" Linked list after moving the last element to 1st position: "); DisplayList(head); }
Output
Move in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert at the beginning def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def getMove(self): temp = self.head secondend = None if not temp or not temp.n: return while temp and temp.n : secondend = temp temp = temp.n secondend.n = None temp.n = self.head self.head = temp if __name__ == '__main__': #Start the Linked List as empty elements llist = LinkedList() #Insert elements llist.Insert(31) llist.Insert(62) llist.Insert(19) llist.Insert(10) print('Linked List after inserting elements:') llist.DisplayList() print() llist.getMove() print() print ('Linked list after moving the end element to 1st position: ') llist.DisplayList()
Output
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; node1->n = (*head_ref); (*head_ref) = node1; } 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() { struct node* head = NULL; struct node* head1 = NULL; Insert(&head, 31); Insert(&head, 62); Insert(&head, 19); Insert(&head, 10); printf(" Elements in Linked List - 1 : ");; DisplayList(head); printf(" "); Insert(&head1, 31); Insert(&head1, 62); Insert(&head1, 19); Insert(&head1, 11); printf(" Elements in Linked List - 2 : ");; DisplayList(head1); getIdentical(head, head1)? printf(" Both the Lists are identical"): printf(" Both the lists are NOT identical"); return 0; }
Output
Identify in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert at the beginning def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def getIdentical(self, list2): x = self.head y = list2.head 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 llist1 = LinkedList() llist2 = LinkedList() #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 ")
Output
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->n = (*head); newnode->p = NULL; if ((*head) != NULL) (*head)->p = newnode; (*head) = newnode; } // 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; struct node* temp = *head; if (*head == NULL) { newnode->p = NULL; *head = newnode; 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() { struct node* head = NULL; //Insert elements InserEnd(&head, 42); InserBeg(&head, 31); InserBeg(&head, 62); InserEnd(&head, 12); InserEnd(&head, 85); InserAf(head, 10); printf("Linked List after inserting elements : "); DisplayList(head); }
Output
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): self.head = None # 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) node1.n = self.head if self.head is not None: self.head.p = node1 self.head = node1 # 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) if self.head is None: self.head = node1 return temp = self.head 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) D_ll.InserAf(D_ll.head, 10) print('Linked List after inserting elements :') D_ll.DisplayList(D_ll.head) print()
Output
22. Add two numbers represented by linked list
Add 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; node1->n = (*head_ref); (*head_ref) = node1; } struct node* newnode(int d) { struct node* node1 = (struct node*)malloc(sizeof(struct node)); node1->d = d; node1->n = NULL; return node1; } struct node* getAdd(struct node* head, struct node* head1) { struct node* final = NULL; struct node *temp, *p = NULL; int carry = 0, add; while (head != NULL || head1 != NULL) { add = carry + (head ? head->d : 0) + (head1 ? head1->d : 0); carry = (add >= 10) ? 1 : 0; add = add % 10; temp = newnode(add); if (final == NULL) final = temp; else p->n = temp; p = temp; if (head) head = head->n; if (head1) head1 = head1->n; } if (carry > 0) temp->n = newnode(carry); return final; } int main() { struct node* head = NULL; struct node* head1 = NULL; struct node* final = NULL; Insert(&head, 4); Insert(&head, 6); Insert(&head, 1); Insert(&head, 0); printf(" Elements in Linked List - 1 : ");; DisplayList(head); printf(" "); Insert(&head1, 3); Insert(&head1, 2); Insert(&head1, 6); Insert(&head1, 1); printf(" Elements in Linked List - 2 : ");; DisplayList(head1); final = getAdd(head, head1); printf(" "); printf(" After adding the numbers present in the list : "); DisplayList(final); }
Output
Add in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert at the beginning def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def getAdd(self, llist1, llist2): 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 add = add if add < 10 else add % 10 temp = node(add) if self.head is None: self.head = temp 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__': llist1 = LinkedList() llist2 = LinkedList() final = LinkedList() #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() final.getAdd(llist1.head, llist2.head) print (" After adding the numbers present in the list : ",end=' ') print() final.DisplayList()
Output
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; }
Output
Create in Python
import gc class node: def __init__(self, d): self.d = d self.n = None class CircularLL: def __init__(self): self.head = None #Traverse & Display def DisplayList(self): x = self.head if(x != None): print() print("Final Circular Linked List : ", end=" ") print() while (True): print(x.d, end=" ") x = x.n if(x == self.head): break print() else: print("The list is empty.") #Insert at beginning def InserBeg(self, ele): newnode = node(ele) if(self.head == None): self.head = newnode newnode.n = self.head return else: x = self.head while(x.n != self.head): x = x.n x.n = newnode newnode.n = self.head self.head = newnode #Insert at end def InserEnd(self, ele): newnode = node(ele) if(self.head == None): self.head = newnode newnode.n = self.head return else: x = self.head while(x.n != self.head): x = x.n x.n = newnode newnode.n = self.head #Inserts a new element at the given pos def InserAf(self, ele, pos): newnode = node(ele) x = self.head count = 0 if(x != None): count += 1 x = x.n while(x != self.head): count += 1 x = x.n if(pos < 1 or pos > (count+1)): print(" Inavalid pos.") elif (pos == 1): if(self.head == None): self.head = newnode self.head.n = self.head else: while(x.n != self.head): x = x.n newnode.n = self.head self.head = newnode x.n = self.head else: x = self.head 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()
Output
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; node1->n = (*head_ref); (*head_ref) = node1; } void getSeg(struct node **head_ref) { struct node *last = *head_ref; struct node *p = NULL; struct node *curr = *head_ref; 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) { *head_ref = curr; 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() { struct node* head = NULL; Insert(&head, 31); Insert(&head, 62); Insert(&head, 19); Insert(&head, 10); printf("Elements in Linked List : ");; DisplayList(head); printf(" "); getSeg(&head); printf(" After segregating even and odd nodes : "); DisplayList(head); }
Output
Segregate in Python
head = None class node: def __init__(self, d): self.d = d self.n =None def getSeg(): global head end = head p = None curr = head 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): head = curr 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): global head node1 = node(new_d) node1.n = head head = node1 def DisplayList(): global head temp = head 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()
Output
25. Reverse linked list
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; node1->n = (*head_ref); (*head_ref) = node1; } static void getRevList(struct node** head_ref) { struct node* p = NULL; struct node* curr = *head_ref; struct node* n = NULL; while (curr != NULL) { n = curr->n; curr->n = p; p = curr; curr = n; } *head_ref = p; } int main() { struct node* head = NULL; Insert(&head, 31); Insert(&head, 62); Insert(&head, 19); Insert(&head, 10); printf("Elements in Linked List : ");; DisplayList(head); printf(" "); getRevList(&head); printf(" Reverse Linked list : "); DisplayList(head); }
Output
Reverse in Python
class node: def __init__(self, d): self.d = d self.n = None #Define class LinkedList class LinkedList: def __init__(self): self.head = None def DisplayList(self): temp = self.head while (temp): print(temp.d) temp = temp.n # Insert def Insert(self, new_d): newnode = node(new_d) newnode.n = self.head self.head = newnode def getRevList(self): p = None curr = self.head while(curr is not None): n = curr.n curr.n = p p = curr curr = n self.head = p if __name__ == '__main__': llist = LinkedList() #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 (" Reverse Linked List : ") 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.
Click here to submit its review with Shiksha Online.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio