0% found this document useful (0 votes)
4 views4 pages

LL Java

basic questions of LinkedList in Java

Uploaded by

Anamika Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

LL Java

basic questions of LinkedList in Java

Uploaded by

Anamika Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

*;
class Node {
int data;
Node next;
Node prev;
Node (int x){
data = x;
next = null;
prev = null;
}

public static int size(Node a ){


int size = 0;
Node temp = a;
while (temp!=null){
temp = temp.next;
size++;
}
return size;
}
public static Node Insertion(Node a, int x ){
Node p = new Node(x);
p.next = a;
p.prev = null;
if(a!= null) a.prev = p;
// a = p;
return p;
}

public static Node insertionAtLast(Node a , int x){


Node last = new Node(x);
last.next = null;
if (a == null){
a = last;
last.prev = null;
return last;
}
Node temp = a;
while(temp.next!=null){
temp =temp.next;
}
temp.next = last;
last.prev = temp;

return a;

public static Node insertionAfterK(Node a , int x , int k){


Node add = new Node(x);

Node temp = a;
Node previous = null;
while(temp!= null && temp.data != k){
temp = temp.next;
}
temp.next = add;
add.prev = temp.next;
add.next = temp.next.next;
return a;
}

public static void Deletion(Node a ){


Node temp =a ;

}
public static void main(String[] args) {
Node a = new Node (5);
Node b = new Node (10);
Node c = new Node (15);
Node d = new Node (20);
a.next = b;
b.prev = a;
b.next = c;
c.prev = b;
c.next = d;
d.prev = c;

int size = size(a);


a = Insertion(a, 0);
a = insertionAtLast(a, 25);
a = insertionAfterK(a, 30, 25 );
Node temp = a ;
while (temp!=null){
System.out.print(temp.data + " ");
temp = temp.next;
}

}
}

/////tenth of october

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */


class Node {
int data;
Node next;
Node prev;
Node (int x){
data = x;
next = null;
prev = null;
}
public static Node returnNode(Node head){
if (head == null){
return null;
}
Node slow = head;
Node fast = head;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;

}
return slow;
}

public static Node reverseLL(Node head){


if (head == null || head.next == null ){
return head;
}
Node prev = null;
Node next = null;
Node temp = head;
while(temp!= null){
next = temp.next;
temp.next = prev;
prev = temp;
temp = next;

}
return prev;

}
public static Node recurseReverse(Node head){
if (head == null || head.next == null){
return head;
}
Node ans = recurseReverse(head.next);
head.next.next = head;
head.next = null;
return ans;
}

Node sortedMerge(Node head1, Node head2) {

Node temp1 = head1;


Node temp2 = head2;
Node res = new Node (-1);
Node ans = res;

while (temp1!= null && temp2 !=null){


if (temp1.data<=temp2.data){
res.next = temp1;
temp1=temp1.next;
}else{
res.next = temp2;
temp2 = temp2.next;
}
res = res.next;

if (temp1== null){
res.next = temp2;
}
if(temp2 == null){
res.next = temp1;
}
}
return ans.next;
}

public static void main (String[] args) throws java.lang.Exception


{
Node a = new Node (5);
Node b = new Node (10);
Node c = new Node (15);
Node d = new Node (20);
a.next = b;
b.prev = a;
b.next = c;
c.prev = b;
c.next = d;
d.prev = c;

Node temp = returnNode(a);


Node ans = reverseLL(a);
while (temp!=null){
System.out.print(temp.data + " ");
temp = temp.next;
}
}

You might also like