11import java .util .*;
2+
23public 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}
0 commit comments