Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 9c3218e

Browse files
committed
update
1 parent 8805953 commit 9c3218e

File tree

7 files changed

+192
-2
lines changed

7 files changed

+192
-2
lines changed

DataAndAlgoL/Chp3LinkedLists/Problem25_28.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public static ListNode findMid(ListNode head){
3232
}
3333
}
3434

35-
return slow;
35+
return slow; //return the slow pointer which points to the middle node
3636
}
3737
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

DataAndAlgoL/Chp3LinkedLists/Problem2_5_6.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
/*
55
* Find the Nth Node from the end of a linked list
6-
* Return that node once foud
6+
* Return that node once found
77
*/
88
public class Problem2_5_6 {
99
public static void main(String[] args) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
}

0 commit comments

Comments
 (0)