Posts

Remove duplicates from sorted LL

void removeDuplicates(struct node* head) {     struct node* current = head;     struct node* next;     if(current == NULL)         return;     while(current->next!= NULL)     {         if(current->data == current->next->data)         {         next=current->next->next;         free(current->next);         current->next=next;         }         else         {         current = current->next;         }     } }   Time Complexity:  O(n)

Print reverse of LL by recursive function

void printReverse(struct node* head) { if(head == NULL) return; else printReverse(head->list); printf("%d",head->data); } Time Complexity:  O(n)

Find intersection point of two LL

Logic: 1) Find the total number of elements in both the LL. 2) Find the difference between number of elements of the two linked list. 3) Move the pointer of that LL which contains more number of elements by the difference. 4) Then move the pointers of both the list and check if nodes present in first LL is same as the node present in second LL. Time Complexity:  O(m+n) if first LL contains m elements and second LL contains n elements. Space Complexity:  O(1)

Insert element in LL in sorted order

Logic: 1) If LL is empty, then insert the node as head and return it. 2) If value of node to be inserted in LL is less than the value of the head node, then insert the node at start and make it head. 3) Otherwise, traverse through the list and find the appropriate place for the node and check if the value of node to be inserted is less than the value of next node, then insert the node before the next node. Implementation in C: #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node* next; }; struct node* newNode(int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data=newData;     newNode->next=NULL;     return newNode; } void print(struct node* n) {     while(n!=NULL)     {         printf("%d    ",n->data);         n=n->next;   ...

Print Middle element of LL

Method 1: Traverse linked list using two pointers. Move first pointer by one and second pointer by two, when second pointer reaches to the end of the list, then first pointer will reach to the middle of the list. Implementation in C: #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node* next; }; int main() {     struct node* head = NULL;     push(&head,1);     push(&head,2);     push(&head,3);     push(&head,4);     push(&head,5);     push(&head,6);     push(&head,7);     printMiddle(head);     return 0; } void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void printMiddle(struct node...

Function to get Nth Node in LL

From beginning of the list: #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node* next; }; int main() {     struct node* head = NULL;     push(&head,8);     push(&head,7);     push(&head,3);     push(&head,4);     push(&head,1);     printf("LinkedList:\n");     print(head);     int value = GetNth(head,0);     printf("\nValue of Nth node is: %d",value);     return 0; } void push(struct node** ref, int newData) {     struct node* newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } int GetNth(struct node* head, int index) {     struct node* current = head;     int count =0;     while(current != NULL)    ...

Deletion in LL

#include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node *next; }; int main() {     struct node *head = NULL;     push(&head,7);     push(&head,8);     push(&head,4);     push(&head,3);     printf("\n Linked List is:\n");     print(head);     delete(&head,8);     printf("\n LinkedList after Deletion of 8: \n");     print(head);     return 0; } void push(struct node** ref, int newData) {     struct node *newNode = (struct node*)malloc(sizeof(struct node));     newNode->data = newData;     newNode->next = (*ref);     (*ref) = newNode; } void delete(struct node** headref,int key) {     struct node* temp = *headref;struct node* prev;     if(temp != NULL && temp->data == key)     {   ...