-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCC.java
97 lines (89 loc) · 2.5 KB
/
CC.java
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import java.util.*;
class CC{
Node head;
class Node{
int data;
Node next;
Node(int data){
this.data=data;
this.next=null;
}
}
public void inputN(int d){
Node nn= new Node(d);
if(head==null){
head=nn;
nn.next=nn;
return;
}
else{
Node temp=head;
while(temp.next!=head){
temp=temp.next;
}
temp.next=nn;
nn.next=head;
return;
}
}
public void printl(){
Node temp = head;
if (temp!= null) {
do {
System.out.print(temp.data + " ");
temp = temp.next;
} while(temp != head);
}
}
public int clength(){
Node temp = head;
int c = 0;
if (temp != null) {
do {
c++;
temp = temp.next;
} while(temp != head);
}
return c;
}
public void cdiv(){
Node fast=head;
Node slow=head;
//VERY IMPORTANT
//the following loop helps in dividing the list into 2 linked list of equal or into the one where the firstone is +1 longer
while(fast.next!=head && fast.next.next!=head){
slow=slow.next;
fast=fast.next.next;
}
Node h1=head;
Node h2=slow.next;
slow.next=h1;//making the list 1 circular
if(fast.next==head){
fast.next=h2;
}else{
fast.next.next=h2;
}
//comments added by me since this one took time to understand 😬
//it really took time 🥲
CC fhalf=new CC(); // Create new instance for first half
CC shalf=new CC(); // Create new instance for second half
fhalf.head = h1; // Assign h1 to fhalf.head
shalf.head = h2; // Assign h2 to shalf.head
System.out.println("First Half:");
fhalf.printl(); // Print first half
System.out.println("\nSecond Half:");
shalf.printl(); // Print second half
}
public static void main(String[] args) {
CC n=new CC();
n.inputN(5);
n.inputN(6);
n.inputN(3);
n.inputN(1);
n.inputN(8);
n.inputN(7);
n.printl();
System.out.println();
n.cdiv();//prints the lls' the original one gets divided into
}
}