Reverse A Doubly Linked List.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void addNode(struct node **head_ref,int pos,int data) { node *current=*head_ref; while(pos) { current=current->next; pos--; } node *tmp=new node; tmp->data=data; tmp->prev=current; tmp->next=current->next; current->next=tmp; } |
Prints the nodes having no siblings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
/* Tree node structure used in the program struct Node { int data; struct Node* left, *right; }; */ void printSibling(struct Node* node) { // Your code here if(node==NULL) return; int v= node->data; Node *lft=node->left; Node *rit=node->right; if(lft==NULL && rit!=NULL) { printf("%d ",rit->data); printSibling(rit); } else if(lft!=NULL && rit==NULL) { printf("%d ",lft->data); printSibling(lft); } else { printSibling(lft); printSibling(rit); } } |
Check A LinkedList is circular or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* Link list Node struct Node { int data; struct Node* next; }; */ /* Should return true if linked list is circular, else false */ bool isCircular(struct Node *head) { // Your code here if(head==NULL) return true; else { Node *node=head; Node *nxt=node->next; while(nxt!=head) { if(nxt==NULL) return false; nxt=nxt->next; } return true; } return true; } |
Find The Middle Node.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/* Link list Node struct Node { int data; struct Node* next; }; */ /* Should return data of middle node. If linked list is empty, then -1*/ int getMiddle(struct Node *head) { // Your code here if(head==NULL) return -1; int total=1; int arr[105]; arr[total]=head->data; head=head->next; while(head!=NULL) { total+=1; arr[total]=head->data; head=head->next; } if(total%2) return arr[(total+1)/2]; else return arr[total/2+1]; } |
Merge two sorted list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
//Merge 2 sorted list /* Link list Node struct Node { int data; struct Node* next; }; */ struct Node* SortedMerge(struct Node* head1, struct Node* head2) { // Your Code Here Node *tmp1=head1,*tmp2=head2; Node *head=new Node; Node *tmp; bool f=0; while((tmp1!=NULL) || (tmp2!=NULL)) { if(tmp1==NULL) { while(tmp2) { tmp->next=tmp2; tmp=tmp->next; tmp2=tmp2->next; } } else if(tmp2==NULL) { while(tmp1) { tmp->next=tmp1; tmp=tmp->next; tmp1=tmp1->next; } } else if((tmp1!=NULL) && (tmp2!=NULL)) { if(tmp1->data<=tmp2->data) { if(f==0) { head->data=tmp1->data; tmp=head; f=1; } else { tmp->next=new Node; tmp=tmp->next; tmp->data=tmp1->data; } tmp1=tmp1->next; } else { if(f==0) { head->data=tmp2->data; tmp=head; f=1; } else { tmp->next=new Node; tmp=tmp->next; tmp->data=tmp2->data; } tmp2=tmp2->next; } } } return head; } |
Delete the middle node.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/* Link list Node struct Node { int data; struct Node* next; }; */ // Deletes middle of linked list and returns head of the modified list struct Node* deleteMid(struct Node *head) { // Your Code Here int arr[1001]; int idx=0; Node *tmp=head; while(tmp) { idx+=1; tmp=tmp->next; } if(idx==0) return head; if(idx==1) return head=NULL; idx=idx/2+1; tmp=head; Node *prev; idx-=1; while(idx) { idx-=1; prev=tmp; tmp=tmp->next; } prev->next=tmp->next; return head; } |
Reverse a doubly linkedlist.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
/* a node of the doubly linked list */ /*struct node { int data; struct node *next; struct node *prev; };*/ void reverse(struct node **head_ref) { // Your code goes here node *tmp=(*head_ref); node *nxt,*prv; node *prev; while(tmp) { nxt=tmp->next; prv=tmp->prev; tmp->prev=nxt; tmp->next=prv; prev=tmp; tmp=nxt; } (*head_ref)=prev; } |