Skip to content

Commit c1ae186

Browse files
authored
Merge branch 'master' into merge_k_ll_improve
2 parents da93623 + fed2d4b commit c1ae186

File tree

5 files changed

+207
-57
lines changed

5 files changed

+207
-57
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@
831831
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
832832
* [CreateAndDetectLoopTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoopTest.java)
833833
* [MergeKSortedLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedListTest.java)
834+
* [MergeSortedSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedListTest.java)
834835
* [QuickSortLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java)
835836
* [ReverseKGroupTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java)
836837
* [RotateSinglyLinkedListsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedListsTest.java)
Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,49 @@
11
package com.thealgorithms.datastructures.lists;
22

3+
/**
4+
* Utility class for merging two sorted singly linked lists.
5+
*
6+
* <p>This class extends the {@link SinglyLinkedList} class to support the merging of two sorted linked lists.
7+
* It provides a static method, `merge`, that takes two sorted singly linked lists, merges them into a single sorted linked list,
8+
* and returns the result.</p>
9+
*
10+
* <p>Example usage:</p>
11+
* <pre>
12+
* SinglyLinkedList listA = new SinglyLinkedList();
13+
* SinglyLinkedList listB = new SinglyLinkedList();
14+
* for (int i = 2; i <= 10; i += 2) {
15+
* listA.insert(i); // listA: 2->4->6->8->10
16+
* listB.insert(i - 1); // listB: 1->3->5->7->9
17+
* }
18+
* SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB);
19+
* System.out.println(mergedList); // Output: 1->2->3->4->5->6->7->8->9->10
20+
* </pre>
21+
*
22+
* <p>The `merge` method assumes that both input lists are already sorted in ascending order.
23+
* It returns a new singly linked list that contains all elements from both lists in sorted order.</p>
24+
*
25+
* @see SinglyLinkedList
26+
*/
327
public class MergeSortedSinglyLinkedList extends SinglyLinkedList {
428

5-
public static void main(String[] args) {
6-
SinglyLinkedList listA = new SinglyLinkedList();
7-
SinglyLinkedList listB = new SinglyLinkedList();
8-
9-
for (int i = 2; i <= 10; i += 2) {
10-
listA.insert(i);
11-
listB.insert(i - 1);
12-
}
13-
assert listA.toString().equals("2->4->6->8->10");
14-
assert listB.toString().equals("1->3->5->7->9");
15-
assert merge(listA, listB).toString().equals("1->2->3->4->5->6->7->8->9->10");
16-
}
17-
1829
/**
19-
* Merge two sorted SingleLinkedList
30+
* Merges two sorted singly linked lists into a single sorted singly linked list.
31+
*
32+
* <p>This method does not modify the input lists; instead, it creates a new merged linked list
33+
* containing all elements from both lists in sorted order.</p>
2034
*
21-
* @param listA the first sorted list
22-
* @param listB the second sored list
23-
* @return merged sorted list
35+
* @param listA The first sorted singly linked list.
36+
* @param listB The second sorted singly linked list.
37+
* @return A new singly linked list containing all elements from both lists in sorted order.
38+
* @throws NullPointerException if either input list is null.
2439
*/
2540
public static SinglyLinkedList merge(SinglyLinkedList listA, SinglyLinkedList listB) {
41+
if (listA == null || listB == null) {
42+
throw new NullPointerException("Input lists must not be null.");
43+
}
44+
2645
Node headA = listA.getHead();
2746
Node headB = listB.getHead();
28-
2947
int size = listA.size() + listB.size();
3048

3149
Node head = new Node();
@@ -40,12 +58,10 @@ public static SinglyLinkedList merge(SinglyLinkedList listA, SinglyLinkedList li
4058
}
4159
tail = tail.next;
4260
}
43-
if (headA == null) {
44-
tail.next = headB;
45-
}
46-
if (headB == null) {
47-
tail.next = headA;
48-
}
61+
62+
// Attach remaining nodes
63+
tail.next = (headA == null) ? headB : headA;
64+
4965
return new SinglyLinkedList(head.next, size);
5066
}
5167
}

src/main/java/com/thealgorithms/datastructures/lists/QuickSortLinkedList.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,36 +104,52 @@
104104

105105
public class QuickSortLinkedList {
106106

107-
private SinglyLinkedList list = null; // Linked list
108-
private Node head = null; // head of the list
109-
// Counstructor
107+
private final SinglyLinkedList list; // The linked list to be sorted
108+
private Node head; // Head of the list
109+
110+
/**
111+
* Constructor that initializes the QuickSortLinkedList with a given linked list.
112+
*
113+
* @param list The singly linked list to be sorted
114+
*/
110115
public QuickSortLinkedList(SinglyLinkedList list) {
111116
this.list = list;
112117
this.head = list.getHead();
113118
}
114119

115-
// Function to sort a linked list using the Quick Sort algorithm
120+
/**
121+
* Sorts the linked list using the QuickSort algorithm.
122+
* The sorted list replaces the original list within the SinglyLinkedList instance.
123+
*/
116124
public void sortList() {
117125
head = sortList(head);
118126
list.setHead(head);
119127
}
120-
// helper function to apply QuickSort to the stored list
121-
public Node sortList(Node head) {
128+
129+
/**
130+
* Recursively sorts a linked list by partitioning it around a pivot element.
131+
*
132+
* <p>Each recursive call selects a pivot, partitions the list into elements less
133+
* than the pivot and elements greater than or equal to the pivot, then combines
134+
* the sorted sublists around the pivot.</p>
135+
*
136+
* @param head The head node of the list to sort
137+
* @return The head node of the sorted linked list
138+
*/
139+
private Node sortList(Node head) {
122140
if (head == null || head.next == null) {
123141
return head;
124142
}
125143

126-
// Choose the first element as the pivot
127144
Node pivot = head;
128145
head = head.next;
129146
pivot.next = null;
130147

131-
Node lessHead = new Node(); // stores the nodes cantaining data less than pivot node
132-
Node lessTail = lessHead; // tail of lessHead
133-
Node greaterHead = new Node(); // stores the nodes cantaining data greater than pivot node
134-
Node greaterTail = greaterHead; // tail of greaterHead
148+
Node lessHead = new Node();
149+
Node lessTail = lessHead;
150+
Node greaterHead = new Node();
151+
Node greaterTail = greaterHead;
135152

136-
// Partition the list around the pivot
137153
while (head != null) {
138154
if (head.value < pivot.value) {
139155
lessTail.next = head;
@@ -145,15 +161,12 @@ public Node sortList(Node head) {
145161
head = head.next;
146162
}
147163

148-
// Seperating lessHead and greaterHead to form two seperate linkedList
149164
lessTail.next = null;
150165
greaterTail.next = null;
151166

152-
// Recursively sort the sublists
153167
Node sortedLess = sortList(lessHead.next);
154168
Node sortedGreater = sortList(greaterHead.next);
155169

156-
// Combine the sorted sublists and pivot
157170
if (sortedLess == null) {
158171
pivot.next = sortedGreater;
159172
return pivot;
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class MergeSortedSinglyLinkedListTest {
9+
10+
@Test
11+
void testMergeTwoSortedLists() {
12+
SinglyLinkedList listA = new SinglyLinkedList();
13+
SinglyLinkedList listB = new SinglyLinkedList();
14+
15+
for (int i = 2; i <= 10; i += 2) {
16+
listA.insert(i);
17+
listB.insert(i - 1);
18+
}
19+
20+
SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB);
21+
assertEquals("1->2->3->4->5->6->7->8->9->10", mergedList.toString(), "Merged list should contain all elements in sorted order.");
22+
}
23+
24+
@Test
25+
void testMergeWithEmptyListA() {
26+
SinglyLinkedList listA = new SinglyLinkedList(); // Empty listA
27+
SinglyLinkedList listB = new SinglyLinkedList();
28+
listB.insert(1);
29+
listB.insert(3);
30+
listB.insert(5);
31+
32+
SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB);
33+
assertEquals("1->3->5", mergedList.toString(), "Merged list should match listB when listA is empty.");
34+
}
35+
36+
@Test
37+
void testMergeWithEmptyListB() {
38+
SinglyLinkedList listA = new SinglyLinkedList();
39+
SinglyLinkedList listB = new SinglyLinkedList(); // Empty listB
40+
listA.insert(2);
41+
listA.insert(4);
42+
listA.insert(6);
43+
44+
SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB);
45+
assertEquals("2->4->6", mergedList.toString(), "Merged list should match listA when listB is empty.");
46+
}
47+
48+
@Test
49+
void testMergeWithBothEmptyLists() {
50+
SinglyLinkedList listA = new SinglyLinkedList(); // Empty listA
51+
SinglyLinkedList listB = new SinglyLinkedList(); // Empty listB
52+
53+
SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB);
54+
assertEquals("", mergedList.toString(), "Merged list should be empty when both input lists are empty.");
55+
}
56+
57+
@Test
58+
void testMergeWithDuplicateValues() {
59+
SinglyLinkedList listA = new SinglyLinkedList();
60+
SinglyLinkedList listB = new SinglyLinkedList();
61+
62+
listA.insert(1);
63+
listA.insert(3);
64+
listA.insert(5);
65+
listB.insert(1);
66+
listB.insert(4);
67+
listB.insert(5);
68+
69+
SinglyLinkedList mergedList = MergeSortedSinglyLinkedList.merge(listA, listB);
70+
assertEquals("1->1->3->4->5->5", mergedList.toString(), "Merged list should include duplicate values in sorted order.");
71+
}
72+
73+
@Test
74+
void testMergeThrowsExceptionOnNullInput() {
75+
SinglyLinkedList listA = null;
76+
SinglyLinkedList listB = new SinglyLinkedList();
77+
listB.insert(1);
78+
listB.insert(2);
79+
80+
SinglyLinkedList finalListA = listA;
81+
SinglyLinkedList finalListB = listB;
82+
assertThrows(NullPointerException.class, () -> MergeSortedSinglyLinkedList.merge(finalListA, finalListB), "Should throw NullPointerException if listA is null.");
83+
84+
listA = new SinglyLinkedList();
85+
listB = null;
86+
SinglyLinkedList finalListA1 = listA;
87+
SinglyLinkedList finalListB1 = listB;
88+
assertThrows(NullPointerException.class, () -> MergeSortedSinglyLinkedList.merge(finalListA1, finalListB1), "Should throw NullPointerException if listB is null.");
89+
}
90+
}

src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
import static org.junit.jupiter.api.Assertions.assertNull;
55

66
import org.junit.jupiter.api.Test;
7+
78
/**
8-
* Test cases for QuickSortLinkedList
9+
* Test cases for QuickSortLinkedList.
910
* Author: Prabhat-Kumar-42
1011
* GitHub: https://github.com/Prabhat-Kumar-42
1112
*/
@@ -16,9 +17,8 @@ public void testSortEmptyList() {
1617
SinglyLinkedList emptyList = new SinglyLinkedList();
1718
QuickSortLinkedList sorter = new QuickSortLinkedList(emptyList);
1819

19-
// Test case: Sorting an empty list should result in an empty list
2020
sorter.sortList();
21-
assertNull(emptyList.getHead());
21+
assertNull(emptyList.getHead(), "Sorted empty list should have no elements.");
2222
}
2323

2424
@Test
@@ -27,10 +27,51 @@ public void testSortSingleNodeList() {
2727
singleNodeList.insert(5);
2828
QuickSortLinkedList sorter = new QuickSortLinkedList(singleNodeList);
2929

30-
// Test case: Sorting a list with a single node should result in the same list
3130
sorter.sortList();
32-
assertEquals(5, singleNodeList.getHead().value);
33-
assertNull(singleNodeList.getHead().next);
31+
assertEquals(5, singleNodeList.getHead().value, "Single node list should remain unchanged after sorting.");
32+
assertNull(singleNodeList.getHead().next, "Single node should not have a next node.");
33+
}
34+
35+
@Test
36+
public void testSortAlreadySorted() {
37+
SinglyLinkedList sortedList = new SinglyLinkedList();
38+
sortedList.insert(1);
39+
sortedList.insert(2);
40+
sortedList.insert(3);
41+
sortedList.insert(4);
42+
sortedList.insert(5);
43+
QuickSortLinkedList sorter = new QuickSortLinkedList(sortedList);
44+
45+
sorter.sortList();
46+
assertEquals("1->2->3->4->5", sortedList.toString(), "Already sorted list should remain unchanged.");
47+
}
48+
49+
@Test
50+
public void testSortReverseOrderedList() {
51+
SinglyLinkedList reverseList = new SinglyLinkedList();
52+
reverseList.insert(5);
53+
reverseList.insert(4);
54+
reverseList.insert(3);
55+
reverseList.insert(2);
56+
reverseList.insert(1);
57+
QuickSortLinkedList sorter = new QuickSortLinkedList(reverseList);
58+
59+
sorter.sortList();
60+
assertEquals("1->2->3->4->5", reverseList.toString(), "Reverse ordered list should be sorted in ascending order.");
61+
}
62+
63+
@Test
64+
public void testSortWithDuplicates() {
65+
SinglyLinkedList listWithDuplicates = new SinglyLinkedList();
66+
listWithDuplicates.insert(3);
67+
listWithDuplicates.insert(1);
68+
listWithDuplicates.insert(3);
69+
listWithDuplicates.insert(2);
70+
listWithDuplicates.insert(2);
71+
QuickSortLinkedList sorter = new QuickSortLinkedList(listWithDuplicates);
72+
73+
sorter.sortList();
74+
assertEquals("1->2->2->3->3", listWithDuplicates.toString(), "List with duplicates should be sorted correctly.");
3475
}
3576

3677
@Test
@@ -48,18 +89,7 @@ public void testSortMultipleElementsList() {
4889
list.insert(6);
4990
QuickSortLinkedList sorter = new QuickSortLinkedList(list);
5091

51-
// Test case: Sorting a list with multiple elements
5292
sorter.sortList();
53-
assertEquals(1, list.getHead().value);
54-
assertEquals(2, list.getHead().next.value);
55-
assertEquals(3, list.getHead().next.next.value);
56-
assertEquals(4, list.getHead().next.next.next.value);
57-
assertEquals(5, list.getHead().next.next.next.next.value);
58-
assertEquals(6, list.getHead().next.next.next.next.next.value);
59-
assertEquals(7, list.getHead().next.next.next.next.next.next.value);
60-
assertEquals(8, list.getHead().next.next.next.next.next.next.next.value);
61-
assertEquals(9, list.getHead().next.next.next.next.next.next.next.next.value);
62-
assertEquals(10, list.getHead().next.next.next.next.next.next.next.next.next.value);
63-
assertNull(list.getHead().next.next.next.next.next.next.next.next.next.next);
93+
assertEquals("1->2->3->4->5->6->7->8->9->10", list.toString(), "List should be sorted in ascending order.");
6494
}
6595
}

0 commit comments

Comments
 (0)