public static int DetectRemove(LinkedList head){
LinkedList slow = head,fast=head;
if(head==null)
return 0;
while(slow != null && fast !=null && fast.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow.value == fast.value){
removeLoop(slow,head);
return 1;
}
}
return 0;
}
public static void removeLoop(LinkedList loopNode , LinkedList head) {
// First find out the length of the loop
int loopLength =1;
LinkedList loopNodeCopy = loopNode;
while(loopNodeCopy.next.value!= loopNode.value){
loopNodeCopy = loopNodeCopy.next;
loopLength++;
}
System.out.println("Loop Length :" + loopLength);
LinkedList ptrToHead = head;
LinkedList ptrToKth = head;
//Move one pointer to Kth node
int k = 0;
while(k<loopLength) {
ptrToKth = ptrToKth.next;
k++;
}
System.out.println("Kth node:" + ptrToKth.value);
// Increment both pointers(from head and one frm Kth node)
// prev to keep track of the last node in d loop
LinkedList prev = null;
while (ptrToHead.value != ptrToKth.value){
prev=ptrToKth;
ptrToHead = ptrToHead.next;
ptrToKth = ptrToKth.next;
}
prev.next=null;
LinkedList.Display(head);
}
Showing posts with label LinkedList. Show all posts
Showing posts with label LinkedList. Show all posts
Sunday, December 4, 2011
Detect and remove Loop in LL
Saturday, December 3, 2011
Reverse alternate k grp of nodes
public static LinkedList reverseK(LinkedList head, int k){
LinkedList prev = null,next=null;
LinkedList curr = head;
int count = 0;
while (curr !=null && count < k) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
count++;
}
if(head!=null)
head.next = curr;
count = 0;
while(curr!=null && count < k-1){
curr = curr.next;
count++;
}
if(curr!=null)
curr.next = reverseK(curr.next,k);
return prev;
}
Reverse in group of size K
public static LinkedList reverseK(LinkedList head, int k){
LinkedList prev = null,next=null;
LinkedList curr = head;
int count = 0;
while (curr !=null && count < k) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
count++;
}
if(next!=null)
head.next = reverseK(next,k);
return prev;
}
Delete nodes which have a greater value on right side
public static LinkedList DeleteRight(LinkedList node){
if(node==null)
return null;
LinkedList prev = null;
LinkedList head = node;
int max = 0;
while(node!=null) {
if(max < node.value){
max = node.value;
prev = node;
}
else
prev.next = node.next;
node=node.next;
}
return head;
}
Pairwise swap of elements
public static LinkedList pairSwap(LinkedList head) {
int temp;
LinkedList start = head;
if(head==null)
return null;
else{
while(head!=null && head.next!=null){
temp = head.value;
head.value = head.next.value;
head.next.value = temp;
head = head.next.next;
}
return start;
}
Addition Linked list !
public static void Add(LinkedList num1, LinkedList num2) {
LinkedList result=null,curr=null,currLast=null;
int carry=0;
while(num1!=null || num2 !=null){
int sum = 0;
sum = carry + (num1!=null?num1.value:0) + (num2!=null?num2.value:0);
carry = sum>=10?1:0;
sum = sum%10;
//new node with sum
currLast = new LinkedList(sum);
//first node : make head to this
if(result==null)
result = currLast;
//else connect to curr next
else
curr.next = currLast;
curr = currLast;
if(num1!=null)
num1 = num1.next;
if(num2!=null)
num2 = num2.next;
}
if(carry > 0)
curr.next = new LinkedList(carry);
}
Friday, December 2, 2011
Reverse a linked list iterative n recursive
public static RevLinkedList reverse(RevLinkedList p){
if(p.next==null){
return p;
}
RevLinkedList q=reverse(p.next);
p.next.next=p;
p.next=null;
return q;
}
Iterative :
public static void reverse(Iterative_reversal p){
Iterative_reversal curr=p.next;
Iterative_reversal head=p;
Iterative_reversal temp=null;
Iterative_reversal prev=p;
while(curr.next!=null)
{
temp=curr.next;
curr.next=prev;
prev=curr;
curr=temp;
}
curr.next=prev;
head.next=null;
Display(curr);
}
Nth node from end of a linked list
public static void Nth(LinkedList node, int k){
LinkedList curr = node ,temp = node;
if(node == null )
return;
while(--k!=0){
if(temp.next!=null)
temp=temp.next;
else{
System.out.println("Less no of nodes");
return;
}
}
while(temp.next!=null){
temp = temp.next;
curr=curr.next;
}
System.out.println(curr.value);
}
Deleting a node from linked list
public static void delete(LinkedList node, int value){
if(node == null)
return;
while(node!=null){
if(node.value == value){
node.value=node.next.value;
node.next=node.next.next;
}
else
node = node.next;
}
}
Subscribe to:
Posts (Atom)