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

Commit a5d76e7

Browse files
committed
Linked List problems
1 parent f42fc4b commit a5d76e7

9 files changed

+4724
-5
lines changed

src/main/java/com/arrays/InversionCountInArray_MergeSort.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,21 @@
2525
public class InversionCountInArray_MergeSort {
2626

2727
// Merge two sorted subarrays arr[low .. mid] and arr[mid + 1 .. high]
28-
public static int merge(int[] arr, int leftIndex, int midIndex, int rightIndex) {
28+
public static int merge(int[] arr, int start, int mid, int end) {
2929
// Left subarray
30-
int[] left = Arrays.copyOfRange(arr, leftIndex, midIndex + 1);
30+
int[] left = Arrays.copyOfRange(arr, start, mid + 1);
3131

3232
// Right subarray
33-
int[] right = Arrays.copyOfRange(arr, midIndex + 1, rightIndex + 1);
33+
int[] right = Arrays.copyOfRange(arr, mid + 1, end + 1);
3434

35-
int leftCursor = 0, rightCursor = 0, current = leftIndex, swaps = 0;
35+
int leftCursor = 0, rightCursor = 0, current = start, swaps = 0;
3636

3737
while (leftCursor < left.length && rightCursor < right.length) {
3838
if (left[leftCursor] <= right[rightCursor])
3939
arr[current++] = left[leftCursor++];
4040
else {
4141
arr[current++] = right[rightCursor++];
42-
swaps += (midIndex + 1) - (leftIndex + leftCursor); /**important*/
42+
swaps += mid + 1 - leftCursor; /**important*/
4343
}
4444
}
4545

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.linkedlist;
2+
3+
import com.linkedlist.model.Node;
4+
5+
public class DetectLoopInLinkedList_LoopInLinkedList {
6+
7+
public static void main(String args[]) {
8+
9+
Node node1 = new Node(4);
10+
Node node2 = new Node(6);
11+
Node node3 = new Node(8);
12+
Node node4 = new Node(12);
13+
Node node5 = new Node(14);
14+
15+
Node startNode = node1;
16+
17+
node1.setNext(node2);
18+
node2.setNext(node3);
19+
node3.setNext(node4);
20+
node4.setNext(node5);
21+
node5.setNext(node2);
22+
23+
System.out.println("Loop:" + detectLoop(startNode));
24+
25+
}
26+
27+
private static boolean detectLoop(Node startNode) {
28+
Node slowPointer = startNode; // Initially ptr1 is at starting location.
29+
Node fastPointer = startNode; // Initially ptr2 is at starting location.
30+
31+
while (fastPointer != null && fastPointer.getNext() != null) { // If ptr2 encounters NULL, it means there is no Loop in Linked list.
32+
slowPointer = slowPointer.getNext(); // ptr1 moving one node at at time
33+
fastPointer = fastPointer.getNext().getNext(); // ptr2 moving two nodes at at time
34+
35+
if (slowPointer == fastPointer) // if ptr1 and ptr2 meets, it means linked list contains loop.
36+
return true;
37+
}
38+
return false;
39+
}
40+
41+
}

src/main/java/com/linkedlist/LoopInLinkedList_FloydCycleDetectionAlgo.html

Lines changed: 2324 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.linkedlist;
2+
3+
import com.linkedlist.model.Node;
4+
5+
/**
6+
* Time complexity : O ( k N ) O(kN) O(kN) where k is the number of linked lists.
7+
* We can merge two sorted linked list in O ( n ) O(n) O(n) time where n is the total number of nodes in two lists
8+
*/
9+
public class MergeTwoSortedLinkedLists {
10+
11+
public static void main (String args[]){
12+
Node node1 = new Node(4);
13+
Node node2 = new Node(6);
14+
Node node3 = new Node(8);
15+
Node node4 = new Node(12);
16+
Node node5 = new Node(14);
17+
18+
node1.setNext(node2);
19+
node2.setNext(node3);
20+
node3.setNext(node4);
21+
node4.setNext(node5);
22+
node5.setNext(null);
23+
24+
Node list1 = node1;
25+
26+
Node node11 = new Node(41);
27+
Node node21 = new Node(61);
28+
Node node31 = new Node(81);
29+
Node node41 = new Node(121);
30+
Node node51 = new Node(141);
31+
32+
node11.setNext(node21);
33+
node21.setNext(node31);
34+
node31.setNext(node41);
35+
node41.setNext(node51);
36+
node51.setNext(null);
37+
38+
Node list2 = node11;
39+
40+
mergeSortedList(list1, list2);
41+
printList( mergeSortedList(list1, list2));
42+
43+
}
44+
45+
private static void printList(Node mergeSortedList) {
46+
while(mergeSortedList!= null) {
47+
System.out.println(mergeSortedList.getData());
48+
mergeSortedList = mergeSortedList.getNext();
49+
}
50+
51+
}
52+
53+
private static Node mergeSortedList(Node list1, Node list2) {
54+
55+
if(list1 == null) return list2;
56+
if(list2 == null) return list1;
57+
58+
Node result ;
59+
60+
if(list1.getData() < list2.getData())
61+
{
62+
result = new Node(list1.getData());
63+
result.setNext(mergeSortedList(list1.getNext(), list2));
64+
}
65+
else
66+
{
67+
result = new Node(list2.getData());
68+
result.setNext(mergeSortedList(list1, list2.getNext()));
69+
}
70+
71+
return result;
72+
73+
}
74+
75+
76+
}
77+

0 commit comments

Comments
 (0)