File tree 7 files changed +192
-2
lines changed
DataAndAlgoL/Chp3LinkedLists 7 files changed +192
-2
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,6 @@ public static ListNode findMid(ListNode head){
32
32
}
33
33
}
34
34
35
- return slow ;
35
+ return slow ; //return the slow pointer which points to the middle node
36
36
}
37
37
}
Original file line number Diff line number Diff line change
1
+ package DataAndAlgoL .Chp3LinkedLists ;
2
+
3
+ /* Displaying a linked list from the end
4
+ *
5
+ * Objective: recursively traverse until the end of the linked list, when coming back start printing the elements
6
+ */
7
+ public class Problem29 {
8
+ public static void main (String [] args ) {
9
+
10
+ }
11
+
12
+ public static void printListEnd (ListNode head ){
13
+ if (head == null ){
14
+ return ;
15
+ }
16
+
17
+ printListEnd (head .getNext ());
18
+ System .out .println (head .getData ());
19
+ }
20
+
21
+ }
Original file line number Diff line number Diff line change 3
3
4
4
/*
5
5
* Find the Nth Node from the end of a linked list
6
- * Return that node once foud
6
+ * Return that node once found
7
7
*/
8
8
public class Problem2_5_6 {
9
9
public static void main (String [] args ) {
Original file line number Diff line number Diff line change
1
+ package DataAndAlgoL .Chp3LinkedLists ;
2
+ /*
3
+ * Check Whether the given linked list length is even or odd
4
+ *
5
+ * OBJECTIVE : USE A POINTER THAT TRAVELS 2X (2 NODES AT A TIME)
6
+ * IF LENGTH IS EVEN POINTER WILL BE NULL, OTHER WISE IT IS NOT NULL
7
+ */
8
+ public class Problem30 {
9
+ public static void main (String [] args ) {
10
+
11
+ }
12
+
13
+ public static int isEvenOrOddLength (ListNode head ){
14
+ while (head != null && head .next != null ){
15
+ head = head .next .next ;
16
+ }
17
+
18
+ if (head == null ){
19
+ return 0 ;
20
+ }
21
+
22
+ return 1 ;
23
+ }
24
+
25
+
26
+ }
Original file line number Diff line number Diff line change
1
+ package DataAndAlgoL .Chp3LinkedLists ;
2
+ /*
3
+ * GIVEN 2 SORTED LIST L1 AND L2 WE NEED TO MERGE THEM INTO A NEW LIST IN SORTED ORDER
4
+ *
5
+ */
6
+ public class Problem32 {
7
+
8
+ public static void main (String [] args ) {
9
+
10
+ }
11
+
12
+ public static ListNode mergeTwoLists (ListNode head1 , ListNode head2 ){
13
+ if (head1 == null ){
14
+ return head2 ;
15
+ }
16
+
17
+ if (head2 == null ){
18
+ return head1 ;
19
+ }
20
+
21
+ ListNode head ;
22
+
23
+ if (head1 .getData () <= head2 .getData ()){
24
+ head =head1 ;
25
+ head .next =mergeTwoLists (head1 .next , head2 );
26
+ }else {
27
+ head =head2 ;
28
+ head .next =mergeTwoLists (head2 .next , head1 );
29
+ }
30
+
31
+ return head ;
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ package DataAndAlgoL .Chp3LinkedLists ;
2
+ /*\
3
+ * Reverse the linked list in pairs. If you have a linked list that holds
4
+ * 1→2→3→4→X, then after the function has been called the linked list would hold 2→1→4→3→X.
5
+ */
6
+ public class Problem34 {
7
+ public static void main (String [] args ) {
8
+
9
+ }
10
+
11
+ //Recursive way
12
+ public static ListNode ReversePairsRec (ListNode head ){
13
+ ListNode temp ;
14
+ if (head == null || head .next == null ){
15
+ return null ; //base case for empty or 1 element list
16
+ }else {
17
+ //Reverse pair first
18
+ temp =head .next ;
19
+ head .next =temp .next ;
20
+ temp .next =head ;
21
+ head =temp ;
22
+
23
+ //call method recursively for the rest of the list
24
+ head .next .next =ReversePairsRec (head .next .next );
25
+ return head ;
26
+
27
+ }
28
+ }
29
+
30
+ //Iterative way
31
+ public static ListNode reversePairIte (ListNode head ){
32
+ ListNode temp1 =null ;
33
+ ListNode temp2 = null ;
34
+
35
+ while (head != null && head .next != null ){
36
+ if (temp1 != null ){
37
+ temp1 .next .next =head .next ;
38
+ }
39
+
40
+ temp1 = head .next ;
41
+ head .next =head .next .next ;
42
+ temp1 .next = head ;
43
+
44
+ if (temp2 ==null ){
45
+ temp2 =temp1 ;
46
+ }
47
+
48
+ head =head .next ;
49
+ }
50
+
51
+ return temp2 ;
52
+ }
53
+ }
Original file line number Diff line number Diff line change
1
+ package DataAndAlgoL .Chp3LinkedLists ;
2
+
3
+ import java .util .List ;
4
+
5
+ /*
6
+ * Split a Circular Linked List into 2 equal parts if number of nodes is even
7
+ * if odd make first list one node extra than second List
8
+ */
9
+
10
+ /*ALGORITHM
11
+ * > Store mid and last pointers of circular list using floyed cycle algorithm
12
+ > Make second half circular
13
+ > Make first half circular
14
+ > set head pointers of the 2 linked lists
15
+ */
16
+ public class Problem38 {
17
+ public static void main (String [] args ) {
18
+
19
+ }
20
+
21
+ public static void SplitList (ListNode head , ListNode head1 , ListNode head2 ){
22
+ ListNode slow = head ;
23
+ ListNode fast = head ;
24
+
25
+ if (head == null ){
26
+ return ;
27
+ }
28
+
29
+ //If there are odd nodes in the circular list then fast.next becomes head
30
+ //If its even nodes fast.next.next= head
31
+ while (fast .next != head && fast .next .next != head ){
32
+ fast = fast .next .next ;
33
+ slow = slow .next ;
34
+ }
35
+
36
+ //If there are even elements in list the move fast pointer
37
+ if (fast .next .next != head ){
38
+ fast =fast .next ;
39
+ }
40
+
41
+ //set head pointer of 1st half
42
+ head1 = head ;
43
+
44
+ //set head pointer of second half
45
+ if (head .next != head ){
46
+ head2 =slow .next ;
47
+ }
48
+
49
+ // make 2nd half circular
50
+ fast .next = slow .next ;
51
+
52
+ //make 1st half circular
53
+ slow .next =head ;
54
+ }
55
+
56
+
57
+ }
You can’t perform that action at this time.
0 commit comments