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

Commit 8805953

Browse files
committed
update on Linked lists
1 parent 0262a92 commit 8805953

9 files changed

+145
-42
lines changed

DataAndAlgoL/Chp3LinkedLists/ExchangeAdjacenNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package DataAndAlgoL.Chp3LinkedLists;
22

3-
//PROBLEM 40 EXCHANGE THE ADJACEN ELEMENTS
3+
//PROBLEM 40 EXCHANGE THE ADJACENT ELEMENTS
44
public class ExchangeAdjacenNodes {
55
public ListNode exchangeAdjNode(ListNode head){
66
ListNode temp = new ListNode(0); //temporary node to point to head

DataAndAlgoL/Chp3LinkedLists/Problem12.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
/*Check whether the given linked list is null-terminated or not
66
* If there is a cycle find the start node of the loop
7+
*
8+
* OBJECTIVE IS TO RETURN THE NODE THAT STARTS THE CYCLE SINCE WE ALREADY KNOW THERE IS CYCLE IN THIS LINKED LIST
79
*/
810
/*SOlUTION: extended version of Floyd cycle finding algorithm
911
* After finding the loop in the
@@ -29,6 +31,7 @@ public static ListNode findBeginOfLoop(ListNode head){
2931
ListNode fast=head;
3032
ListNode slow=head;
3133
boolean loopExists=false;
34+
//We find if a loop exists
3235
while(fast != null && fast.getNext() != null){ //while linked list is not empty and head is not null
3336
fast= fast.getNext().getNext();// double jump, travels at 2x rather than once
3437
slow= slow.getNext();
@@ -38,14 +41,15 @@ public static ListNode findBeginOfLoop(ListNode head){
3841
}
3942
}
4043

44+
//once we find that the loop exists we start slow pointer from head and iterate once as well as the fast pointer but at its current position
4145
if(loopExists){ //while loop is is
4246
slow=head; //get slow back to head
4347
while(slow != fast){ //while slow and fast are not equal now both travel one jump at time until both pointers are in the loop at the start loop position
4448
slow=slow.getNext();
4549
fast=fast.getNext();
4650
}
47-
48-
return fast; //return the pointer where the cycle starts
51+
//once both slow and fast are equals, we return the node fast which indicates its the first node that starts the cycle
52+
return fast;
4953
}
5054

5155
return null;

DataAndAlgoL/Chp3LinkedLists/Problem15.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package DataAndAlgoL.Chp3LinkedLists;
22
/*
33
* Check whether the given linked list is NULL-terminated. If there is a cycle, find the length of the loop.
4+
*
5+
* OBJECTIVE IS TO FIND THE AMOUNT OF NODES IN THE LOOP SIDE,
6+
* WE NEED TO FIND IF THERE IS A LOOP. WE CAN START COUNTING HOW MANY NODES ARE IN THE LOOP
7+
* BY HAVING ONE OF THE POINTERS TRAVELING THROUGH THE LOOP UNTIL IT FINDS THE OTHER POINTER THAT STAYED STATIONARY
48
*/
59
/*
610
* This solution is also an extension of the basic cycle detection problem. After finding the

DataAndAlgoL/Chp3LinkedLists/Problem16.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package DataAndAlgoL.Chp3LinkedLists;
22
/* Insert a node in a sorted linked list
33
* Solution: Traverse the list and find a position for the element and insert it.
4+
*
5+
* OBJECTIVE -> COMPARE BOTH THE NODE WE WANT TO INSERT PLUS EVERY NODE IN THE LIST
6+
* UNTIL WE FIND THE NODE BIGGER THAN THE NEW NODE
7+
* THEN WE SWAP THEM
48
*/
59
public class Problem16 {
610
public static void main(String[] args) {

DataAndAlgoL/Chp3LinkedLists/Problem2.java

Lines changed: 0 additions & 37 deletions
This file was deleted.

DataAndAlgoL/Chp3LinkedLists/Problem24.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@ public static void main(String[] args) {
55

66
}
77

8+
//O(max(m,n)) is the run time complexity for this problem
89
public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
910
int l1=0;
1011
int l2=0;
1112
int diff=0;
1213
ListNode head1= headA;
1314
ListNode head2= headB;
14-
15+
//Finding the length of both list and see which one is bigger leads to O(max(n,m))
16+
//O(n) Iterates throught one list of n inputs
1517
while(head1 != null){
1618
l1++;
1719
head1= head1.next;
1820
}
19-
21+
// O(m) Iterates throught a second list of m inputs
2022
while(head2 != null){
2123
l2++;
2224
head2=head2.next;
2325
}
2426

27+
//Taking the different is done in O(1)
2528
if(l1 < l2){
2629
head1=headB;
2730
head2=headA;
@@ -32,6 +35,7 @@ public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
3235
diff= l1-l2;
3336
}
3437

38+
// O(diff) for making diff steps in longer list
3539
for(int i=0; i< diff; i++){
3640
head1= head1.next;
3741
while(head1 != null && head2 != null){
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package DataAndAlgoL.Chp3LinkedLists;
2+
3+
/*
4+
* How can we find the middle of a linked list !!
5+
* With just one scan which is the faster algorithm
6+
*
7+
* OBJECTIVE -> USE 2 POINTERS, ONE GOES TWICE FAST THAN THE OTHER ONCE FAST NODE REACHES NULL POINTER
8+
* SLOW POINTER WILL BE AT THE MIDDLE POSITION
9+
*
10+
* NOTE: IF LIST HAS AN EVEN LENGTH MIDDLE NODE WILL BE N/2
11+
*/
12+
public class Problem25_28 {
13+
public static void main(String[] args) {
14+
15+
}
16+
17+
public static ListNode findMid(ListNode head){
18+
ListNode fast= head;
19+
ListNode slow= head;
20+
int i=0;
21+
22+
//Loops until the tail is reached
23+
while(fast.next!= null){
24+
if(i==0){ //increment only 1st pointer
25+
fast=fast.next;
26+
i=1;
27+
}else if(i==1){
28+
//increement both pointers
29+
fast=fast.next;
30+
slow= slow.next;
31+
i=0;
32+
}
33+
}
34+
35+
return slow;
36+
}
37+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package DataAndAlgoL.Chp3LinkedLists;
2+
3+
4+
/*
5+
* Find the Nth Node from the end of a linked list
6+
* Return that node once foud
7+
*/
8+
public class Problem2_5_6 {
9+
public static void main(String[] args) {
10+
11+
}
12+
13+
//solution from problem 5
14+
public static ListNode NthNodeFromEnd(ListNode head, int NthNode){
15+
ListNode pTemp=head; //temporary node
16+
ListNode pNthNode= null; //counts the number of nodes
17+
// pNthNode only starts movinf after pTemp has made n moves
18+
19+
for(int i=1; i<NthNode; i++){
20+
if(pTemp != null){
21+
pTemp= pTemp.getNext(); //if temporary pointer is not null get the next node until we find the end of the list
22+
}
23+
}
24+
//pTemp node moves forward until it reaches the end of the list
25+
while(pTemp!=null){
26+
if(pNthNode==null){
27+
pNthNode= head;
28+
}else{
29+
pNthNode=pNthNode.getNext();
30+
}
31+
32+
pTemp= pTemp.getNext();
33+
}
34+
35+
if(pNthNode != null){
36+
return pNthNode;
37+
}
38+
39+
return null;
40+
}
41+
42+
//Using Recursion which is O(2n) = O(n), however small than previous one due to recursion call
43+
public ListNode NthNodeFromEnd2(ListNode head, int Nth){
44+
int counter=0;//counts the ietarions up to the node
45+
if(head != null){
46+
NthNodeFromEnd2(head.next, Nth); //recursion call, where if head is not equal to null, increase counter
47+
counter++;
48+
if(Nth == counter){// If counter is equal to Nth then return the head
49+
return head;
50+
}
51+
}
52+
53+
return null; //return null if there is no element in linked list
54+
}
55+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package DataAndAlgoL.Chp3LinkedLists;
2+
3+
/*
4+
* EXERCISE 7 BUT SOLUTION FOR EXERCISE 10
5+
* Check whether the given linked list is either NULL-terminated or ends in a cycle
6+
*/
7+
public class Problem7_10 {
8+
public static void main(String[] args) {
9+
10+
}
11+
12+
//O(n) Solution -> Original Floyd Cycle algorithm (slow and fast pointer), once they enter a cycle they are expected to meet, which denotes a loop
13+
public static boolean findLoopIfExists(ListNode head){
14+
ListNode slow= head;
15+
ListNode fast= head;
16+
17+
//If the 2 next pointers of fast are not null
18+
//then fast jumps 2 nodes and slow jumps one node
19+
while(fast.next != null && fast.next.next != null){
20+
fast= fast.next.next;
21+
slow=slow.next;
22+
23+
if(slow == fast){ //if there is no null pointers found; however slow and fast are equal, then there is a cycle and return true for existence of a loop
24+
return true;
25+
}
26+
}
27+
28+
return false;
29+
}
30+
31+
32+
}

0 commit comments

Comments
 (0)