Saturday, December 9, 2017

Linkedllist Based Problems

Find if there is a loop in a linkedlist:

  1. Loop in a linked list
  2. Calculate node that causes loop in a linked list
  3. Calculate length of the linked list that contains loop
  4. Find kth node from the end of linked list that contains loop
  5. Reverse a Single Linked List: Iterative Procedure
  6. Reverse a Single Linked List: Recursive Procedure
  7. K-Reverse linked list
https://crackinterviewtoday.wordpress.com/2010/03/16/loop-in-a-linked-list/

Write a function to get the intersection point of two Linked Lists.


Given two linked lists find if they are identical:
verify(LinkedList a , LinkedList b){  
if ( a== null && b == null) return true;
if ( a != null && b == null || a == null && b != null) return false;
 if( a.data != b.data) return false;
    return verify(a.next,b.next);
 }  
Merging two sorted linked lists
public Node mergeList(Node a, Node b){  
     Node result = null;  
     if(a==null)  return b;  
     if(b==null)  return a;  
     if(a.data <= b.data){  
       result =a;  
       result.next = mergeList(a.next,b);  
     }  
     else  
     {  
       result =b;  
       result.next = mergeList(b.next,a);  
     }  
     return result;  
   }  
Reversing LinkedList
 public void reverse()   
   {   
     Node current = head;   
     head = null ;  
     while(current != null)   
     {   
       Node temp = current;   
       //Interchange the pointers   
       current = current.next;   
       temp.next = head;   
       head = save;   
     }   
   }                                    
  }
SINGLY LINKEDLIST

 class Node{                                  
  Node Node;                                   
   int data;                                      
  } 

operations

HEAD                                 
-insertAtFirst/Index/Last()                                 
-iterate/display                                     
-deletion

insertAtFirst()

Create Node
if head is null
head = n;
else
n.next = head;
head = n;

insertAtIndex(int val,int pos)

Node currentNode = head;
Create Node n;
int pos = pos -1
for(int i=0;i< list.length();i++){
if(i== pos){
Node temp = currentNode.next
currentNode = n;
currentNode = temp;
}
currentNode = currentNode.next;
}

insertAtLast()

Create Node

while(node.next !=null ){
}
removeFirst()
removeLast()
remove()

DOUBLE LINKED LIST

 class Node{                                  
  Node prev;  
   Node next                                   
   int data;                                      
  }

operations

HEAD                               
-INSERT                                  
-DELETE                                      
-ITERATE 

insertAtFirst()

Create Node
if head is null
head = n;
else
n.next = head;
head = n;

insertAtIndex()

insertAtLast()

removeFirst()

removeLast()

remove()

 public void deleteNode(Node node){  
  Node temp = node.next;  
  node.data = temp.data;  
  node.next = temp.next;  
  //do nothing with temp and let the garbage collector remove it  
  }

CIRCULAR LINKED LIST

 class Node{                                  
  Node prev;                                  
 int data;                                      
  } 

HEAD                               
-                                  
-                                      
- 

No comments:

Post a Comment