Acc2 - Vit Ap
Acc2 - Vit Ap
Intuition:-
Key Points:
● Node Class:
○ A Node class is defined with two fields:
○ data: The integer data stored in the node.
○ next: A reference to the next node in the linked list.
○ The constructor of the Node class initializes the data and
sets next to null.
● Linked List Construction:
○ The insert(int n) method adds a new node with data n at
the end of the linked list. If the list is empty (i.e., head is
null), it initializes the list with the new node as the head.
○ The method traverses the list to find the last node and
appends the new node to it.
● Creating a Loop in the Linked List:
○ The createLoop(int a, int b) method creates a loop in the
linked list. Here:
■ a is the data value of the node where the loop will
point to.
■ b is the number of steps from the head node at
which the loop will be created.
○ It first locates the node with data a, then traverses b
steps from the head node and sets the next pointer of the
node at position b to point to the node with data a,
creating a loop.
Code:
import java.util.*;
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
class Main{
static Node head=null;
static void insert(int n){
Node newNode = new Node(n);
if(head==null){
head=newNode;
}
else{
Node curr = head;
while( curr.next != null ){
curr = curr.next;
}
curr.next = newNode;
}
}
static boolean createLoop(int a,int b){
int count = 0;
Node n1 = head;
Node n2 = head;
while( n1 != null && n1.data != a){
n1 = n1.next;
}
if(n1 == null)
return false;
while(count < b && n2!=null){
n2 = n2.next;
count++;
}
n2.next = n1;
return true;
}
static boolean detetctloop(Node head){
Node slow = head;
Node fast = head;
*If you are doing a few more problem statements, that is appreciable.
1. Detect Loop
2. Find the starting node of the loop
3. Find the Length of the Loop in Linked List
Intuition:-
Given a biotonic doubly linked list. The task is to sort the given
linked list. A biotonic doubly linked list is a doubly linked list that is first
increasing and then decreasing. A strictly increasing or a strictly
decreasing list is also a biotonic doubly linked list.
Key Points:
● Two Pointers:
○ first: Start at the head of the list (beginning of the
increasing part).
○ last: Starts at the tail of the list (end of the decreasing
part).
● Merging Logic:
○ Compare first.data and last.data:
○ Append the smaller value to the result list.
○ Move the respective pointer (first forward or last
backward).
● In-Place Reorganization:
○ Adjust the next and prev pointers to build the sorted list.
○ No extra space is required.
● Handles All Cases:
○ Works for fully increasing, fully decreasing, and edge
cases like single-node or empty lists.
Code:
import java.util.Scanner;
class Main{
static node head=null;
static class node{
int data;
node next;
node prev;
node(int n){
data=n;
next=null;
prev=null;
}
}
static void insert(int n){
node newnode = new node(n);
if(head==null) head=newnode;
else{
node cur=head;
while(cur.next!=null) cur=cur.next;
cur.next=newnode;
newnode.prev=cur;
}
}
static void bit(){
node first=head;
node last=head;
node res=null;
node resend=null;
while(last.next!=null) last=last.next;
while(first!=last){
if(first.data<=last.data){
if(res==null){
res=resend=first;
first=first.next;
}
else{
node cur=first.next;
resend.next=first;
first.prev=resend;
cur.prev=null;
first=cur;
resend=resend.next;
}
}
else{
if(res==null){
res=resend=last;
last=last.prev;
}
else{
node cur=last.prev;
resend.next=last;
last.prev=resend;
cur.next=null;
last=cur;
resend=resend.next;
}
}
}
resend.next=first;
first.prev=resend;
head=res;
}
static void display(){
node cur=head;
while(cur!=null){
System.out.print(cur.data+"-->");
cur=cur.next;
}
System.out.print("null");
}
public static void main(String ar[]){
Scanner sw = new Scanner(System.in);
int n=sw.nextInt();
for(int i=0;i<n;i++){
insert(sw.nextInt());
}
display();
bit();
System.out.println();
display();
}
}
Time Complexity : O(N)
Space Complexity : O(constant)
Online Platform Reference Links:
*If you are doing a few more problem statements, that is appreciable.
Intuition:-
Given a Linked List of integers, The task is to modify the linked list
such that all even numbers appear before all the odd numbers in the
modified linked list. Also, preserve the order of even and odd numbers.The
task is to segregate the nodes of a singly linked list into two groups:
The resulting list will first contain all even nodes, followed by all odd
nodes, while preserving the original relative order within each group.
Key Points:
import java.util.Scanner;
class Main{
static node head=null;
static class node{
int data;
node next;
node(int n){
data=n;
next=null;
}
}
static void insert(int n){
node newnode = new node(n);
if(head==null) head=newnode;
else{
node cur=head;
while(cur.next!=null) cur=cur.next;
cur.next=newnode;
}
}
static void seg(){
node es=null;
node ee=null;
node os=null;
node oe=null;
node cur=head;
while(cur!=null){
if(cur.data%2==0){
if(es==null) es=ee=cur;
else ee=ee.next=cur;
}
else{
if(os==null) os=oe=cur;
else oe=oe.next=cur;
}
cur=cur.next;
}
if(es==null) head=os;
else{
head=es;
ee.next=os;
oe.next=null;
}
}
}
}
*If you are doing a few more problem statements, that is appreciable.
Intuition:-
Given a doubly linked list, The task is to sort the doubly linked list in
non-decreasing order using merge sort.. Also, preserve the order of even
and odd numbers.The goal is to sort a doubly linked list (DLL) using the
merge sort algorithm. Merge sort is a divide-and-conquer algorithm that
splits the list into smaller parts, sorts them individually, and merges them
back to create a fully sorted list. The doubly linked list’s ability to traverse
both forward and backward simplifies the merging process.
Key Points:
Code:
import java.util.Scanner;
class Main{
static node head=null;
static class node{
int data;
node next;
node prev;
node(int n){
data=n;
next=null;
prev=null;
}
}
static void insert(int n){
node newnode = new node(n);
if(head==null) head=newnode;
else{
node cur=head;
while(cur.next!=null) cur=cur.next;
cur.next=newnode;
newnode.prev=cur;
}
}
static node sort(node first){
if(first==null||first.next==null) return first;
node second=split(first);
first=sort(first);
second=sort(second);
return merge(first,second);
}
static node split(node first){
node fast=first;
node slow=first;
while(fast.next!=null&&fast.next.next!=null){
fast=fast.next.next;
slow=slow.next;
}
node temp=slow.next;
slow.next=null;
return temp;
}
static node merge(node first,node second){
if(first==null) return second;
if(second==null) return first;
if(first.data<=second.data){
first.next=merge(first.next,second);
first.next.prev=first;
first.prev=null;
return first;
}
else{
second.next=merge(first,second.next);
second.next.prev=second;
second.prev=null;
return second;
}
}
static void display(){
node cur=head;
while(cur!=null){
System.out.print(cur.data+"-->");
cur=cur.next;
}
System.out.print("null");
}
public static void main(String ar[]){
Scanner sw = new Scanner(System.in);
int n=sw.nextInt();
for(int i=0;i<n;i++){
insert(sw.nextInt());
}
head=sort(head);
display();
}
}
*If you are doing a few more problem statements, that is appreciable.