Skip to content

Commit baec7ef

Browse files
committed
[FEAT]:PrettyFying the Code
1 parent bcc9072 commit baec7ef

34 files changed

+1231
-1095
lines changed

LinkedList/JAVA/FloydCycleDetection.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,46 +29,42 @@ If the fast pointer reaches null(the value that is pointed to by the last node),
2929
3030
Below is the code snippet to implement Floyd's Cycle Detection
3131
*/
32-
public class FloydCycleDetection
33-
{
32+
public class FloydCycleDetection {
3433
private ListNode head;
35-
private class ListNode<T>
36-
{
34+
35+
private class ListNode<T> {
3736
private T data;
3837
private ListNode<T> next;
39-
public ListNode(T data)
40-
{
38+
39+
public ListNode(T data) {
4140
this.data = data;
4241
this.next = null;
4342
}
4443
}
44+
4545
public boolean detectLoop() // Detecting whether there is a loop using Floyd's Cycle Detection
4646
{
4747
ListNode fastPointer = head;
4848
ListNode slowPointer = head;
49-
while(fastPointer != slowPointer && fastPointer.next != null)
50-
{
49+
while (fastPointer != slowPointer && fastPointer.next != null) {
5150
fastPointer = fastPointer.next.next;
5251
slowPointer = slowPointer.next;
53-
if(slowPointer == fastPointer)
52+
if (slowPointer == fastPointer)
5453
return true;
5554
}
5655
return false;
5756
}// End of detectLoop()
58-
57+
5958
public ListNode startingNodeOfLoop() // Returning the starting node of the loop
6059
{
6160
ListNode fastPointer = head;
6261
ListNode slowPointer = head;
63-
while(fastPointer != slowPointer && fastPointer.next != null)
64-
{
62+
while (fastPointer != slowPointer && fastPointer.next != null) {
6563
fastPointer = fastPointer.next.next;
6664
slowPointer = slowPointer.next;
67-
if(slowPointer == fastPointer)
68-
{
65+
if (slowPointer == fastPointer) {
6966
ListNode temp = head;
70-
while(temp != slowPointer)
71-
{
67+
while (temp != slowPointer) {
7268
temp = temp.next;
7369
slowPointer = slowPointer.next;
7470
}
@@ -77,5 +73,5 @@ public ListNode startingNodeOfLoop() // Returning the starting node of the loop
7773
}
7874
return null;
7975
}// End of startingNodeOfLoop()
80-
76+
8177
}
Lines changed: 79 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
11
import java.util.*;
2+
23
public class LinkedListNode<T> {
34
public T data;
45
public LinkedListNode<Integer> next;
5-
//Start of Singly LinkedList
6+
7+
// Start of Singly LinkedList
68
/******************************************************************************************************************** */
7-
public LinkedListNode(T Data,LinkedListNode<Integer>Next) {
9+
public LinkedListNode(T Data, LinkedListNode<Integer> Next) {
810
this.data = Data;
911
this.next = Next;
1012
}
1113

1214
public LinkedListNode(T Data) {
1315
this.data = Data;
1416
}
15-
public LinkedListNode(){
17+
18+
public LinkedListNode() {
1619

1720
}
18-
public static void printList(LinkedListNode<Integer>head) {
19-
LinkedListNode<Integer>temp = head;
21+
22+
public static void printList(LinkedListNode<Integer> head) {
23+
LinkedListNode<Integer> temp = head;
2024
while (temp != null) {
2125
System.out.print(temp.data + " ");
2226
temp = temp.next;
2327
}
2428
System.out.println();
2529
}
26-
public static void printrecursive(LinkedListNode<Integer>head){
27-
if(head==null){
28-
System.out.println( );
30+
31+
public static void printrecursive(LinkedListNode<Integer> head) {
32+
if (head == null) {
33+
System.out.println();
2934
return;
3035
}
31-
System.out.print(head.data+" ");
36+
System.out.print(head.data + " ");
3237
printrecursive(head.next);
3338
}
3439

35-
public static LinkedListNode<Integer>takeInput() {
40+
public static LinkedListNode<Integer> takeInput() {
3641
Scanner input = new Scanner(System.in);
3742
int num = input.nextInt();
38-
LinkedListNode<Integer>head = null, tail = null;
43+
LinkedListNode<Integer> head = null, tail = null;
3944
while (num != -1) {
40-
LinkedListNode<Integer>temp = new LinkedListNode<Integer>(num);
45+
LinkedListNode<Integer> temp = new LinkedListNode<Integer>(num);
4146
if (head == null) {
4247
head = temp;
4348
tail = temp;
@@ -51,11 +56,11 @@ public static void printrecursive(LinkedListNode<Integer>head){
5156
return head;
5257
}
5358

54-
public static LinkedListNode<Integer>InsertNode(LinkedListNode<Integer>head, int position, int value) {
55-
LinkedListNode<Integer>temp = head;
59+
public static LinkedListNode<Integer> InsertNode(LinkedListNode<Integer> head, int position, int value) {
60+
LinkedListNode<Integer> temp = head;
5661
int count = 0;
5762
if (position == 0) {
58-
LinkedListNode<Integer>insert = new LinkedListNode<Integer>(value,temp);
63+
LinkedListNode<Integer> insert = new LinkedListNode<Integer>(value, temp);
5964
temp = insert;
6065
return temp;
6166
} else {
@@ -70,103 +75,104 @@ public static void printrecursive(LinkedListNode<Integer>head){
7075
return head;
7176
} else {
7277
if (temp.next == null) {
73-
LinkedListNode<Integer>Insert = new LinkedListNode<Integer>(value, null);
78+
LinkedListNode<Integer> Insert = new LinkedListNode<Integer>(value, null);
7479
temp.next = Insert;
7580
} else {
76-
LinkedListNode<Integer>insert = new LinkedListNode<Integer>(value,temp.next);
81+
LinkedListNode<Integer> insert = new LinkedListNode<Integer>(value, temp.next);
7782
temp.next = insert;
7883
}
7984
return head;
8085
}
8186
}
8287
}
83-
public static LinkedListNode<Integer>DeleteNode(LinkedListNode<Integer>head,int position){
84-
LinkedListNode<Integer>temp=head;
85-
if(position==0){
86-
head=temp.next;
88+
89+
public static LinkedListNode<Integer> DeleteNode(LinkedListNode<Integer> head, int position) {
90+
LinkedListNode<Integer> temp = head;
91+
if (position == 0) {
92+
head = temp.next;
8793
return head;
88-
}
89-
else{
90-
int count=0;
91-
while(count!=position-1){
92-
if(temp.next==null){
94+
} else {
95+
int count = 0;
96+
while (count != position - 1) {
97+
if (temp.next == null) {
9398
break;
9499
}
95-
temp=temp.next;
100+
temp = temp.next;
96101
count++;
97102
}
98-
if(position>count&&temp.next==null){
103+
if (position > count && temp.next == null) {
104+
return head;
105+
} else {
106+
temp.next = temp.next.next;
99107
return head;
100-
}
101-
else{
102-
temp.next=temp.next.next;
103-
return head;
104108
}
105109
}
106110
}
107-
public static LinkedListNode<Integer>InsertRecursive(LinkedListNode<Integer>head, int element,int position){
108-
if(head==null&&position>=0){
111+
112+
public static LinkedListNode<Integer> InsertRecursive(LinkedListNode<Integer> head, int element, int position) {
113+
if (head == null && position >= 0) {
109114
return head;
110115
}
111-
if(position==0){
112-
LinkedListNode<Integer>newnode=new LinkedListNode<Integer>(element);
113-
newnode.next=head;
116+
if (position == 0) {
117+
LinkedListNode<Integer> newnode = new LinkedListNode<Integer>(element);
118+
newnode.next = head;
114119
return newnode;
115-
}
116-
else{
117-
head.next=InsertRecursive(head.next,element,position-1);
120+
} else {
121+
head.next = InsertRecursive(head.next, element, position - 1);
118122
return head;
119123
}
120124
}
121-
public static LinkedListNode<Integer>DeleteRecursive(LinkedListNode<Integer>head,int position){
122-
if(head==null&&position>=0){
125+
126+
public static LinkedListNode<Integer> DeleteRecursive(LinkedListNode<Integer> head, int position) {
127+
if (head == null && position >= 0) {
123128
return head;
124129
}
125-
if(position==0){
130+
if (position == 0) {
126131
return head.next;
127-
}
128-
else{
129-
head.next=DeleteRecursive(head.next,position-1);
132+
} else {
133+
head.next = DeleteRecursive(head.next, position - 1);
130134
return head;
131135
}
132136
}
133-
public static LinkedListNode<Integer>ReverseRevursive(LinkedListNode<Integer>head){
134-
if(head==null||head.next==null){
137+
138+
public static LinkedListNode<Integer> ReverseRevursive(LinkedListNode<Integer> head) {
139+
if (head == null || head.next == null) {
135140
return head;
136141
}
137-
LinkedListNode<Integer>Smallhead=ReverseRevursive(head.next);
138-
LinkedListNode<Integer>tail=Smallhead;
139-
while(tail.next!=null){
140-
tail=tail.next;
142+
LinkedListNode<Integer> Smallhead = ReverseRevursive(head.next);
143+
LinkedListNode<Integer> tail = Smallhead;
144+
while (tail.next != null) {
145+
tail = tail.next;
141146
}
142-
tail.next=head;
143-
head.next=null;
147+
tail.next = head;
148+
head.next = null;
144149
return Smallhead;
145150
}
146-
public static LinkedListNode<Integer>ReverseBest(LinkedListNode<Integer>head){
147-
if(head==null||head.next==null){
151+
152+
public static LinkedListNode<Integer> ReverseBest(LinkedListNode<Integer> head) {
153+
if (head == null || head.next == null) {
148154
return head;
149155
}
150-
LinkedListNode<Integer>smallhead=ReverseBest(head.next);
151-
LinkedListNode<Integer>tail=head.next;
152-
tail.next=head;
153-
head.next=null;
156+
LinkedListNode<Integer> smallhead = ReverseBest(head.next);
157+
LinkedListNode<Integer> tail = head.next;
158+
tail.next = head;
159+
head.next = null;
154160
return smallhead;
155161
}
156-
public static void FindMid(LinkedListNode<Integer>head){
157-
LinkedListNode<Integer>slow=head;
158-
LinkedListNode<Integer>fast=head;
159-
if(head==null){
160-
System.out.println(-1);
161-
}
162-
else{
163-
while(fast!=null&&fast.next!=null){
164-
slow=slow.next;
165-
fast=fast.next.next;
166-
}
167-
System.out.println(slow.data);
162+
163+
public static void FindMid(LinkedListNode<Integer> head) {
164+
LinkedListNode<Integer> slow = head;
165+
LinkedListNode<Integer> fast = head;
166+
if (head == null) {
167+
System.out.println(-1);
168+
} else {
169+
while (fast != null && fast.next != null) {
170+
slow = slow.next;
171+
fast = fast.next.next;
172+
}
173+
System.out.println(slow.data);
168174
}
169175
}
170-
//End of Singly LinkedList
176+
// End of Singly LinkedList
171177
/*********************************************************************************************************************/
172178
}

LinkedList/JAVA/Problems/addNumbersLeetcode.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
11
/**
22
* Definition for singly-linked list.
33
* public class ListNode {
4-
* int val;
5-
* ListNode next;
6-
* ListNode() {}
7-
* ListNode(int val) { this.val = val; }
8-
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
99
* }
1010
*/
1111
class Solution {
12-
public class ListNode{
12+
public class ListNode {
1313
public int val;
1414
public ListNode next;
15-
public ListNode(){
1615

16+
public ListNode() {
17+
18+
}
19+
20+
public ListNode(int val) {
21+
this.val = val;
22+
}
23+
24+
ListNode(int val, ListNode next) {
25+
this.val = val;
26+
this.next = next;
1727
}
18-
public ListNode(int val) { this.val = val; }
19-
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
2028
}
29+
2130
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
2231
ListNode temp1 = new ListNode(0);
2332
ListNode temp2 = temp1;
2433
int carryOver = 0;
25-
while(l1 != null || l2!= null)
26-
{
27-
int digit1 = (l1 != null)?l1.val:0;
28-
int digit2 = (l2 != null)?l2.val:0;
34+
while (l1 != null || l2 != null) {
35+
int digit1 = (l1 != null) ? l1.val : 0;
36+
int digit2 = (l2 != null) ? l2.val : 0;
2937
int sum = carryOver + digit1 + digit2;
30-
carryOver = sum/10;
31-
temp2.next = new ListNode(sum%10);
38+
carryOver = sum / 10;
39+
temp2.next = new ListNode(sum % 10);
3240
temp2 = temp2.next;
33-
if(l1 != null)
41+
if (l1 != null)
3442
l1 = l1.next;
35-
if(l2 != null)
43+
if (l2 != null)
3644
l2 = l2.next;
3745
}
38-
if(carryOver>0)
46+
if (carryOver > 0)
3947
temp2.next = new ListNode(carryOver);
4048
return temp1.next;
4149
}

0 commit comments

Comments
 (0)