Add tests for SinglyLinkedList (#3913)

This commit is contained in:
Trần Quang Dự
2023-03-12 18:49:17 +07:00
committed by GitHub
parent a7e76c57a0
commit 2418604f7a
2 changed files with 120 additions and 0 deletions

View File

@ -54,6 +54,24 @@ public class SinglyLinkedList extends Node {
return false;
}
/**
* Return the node in the middle of the list
* If the length of the list is even then return item number length/2
* @return middle node of the list
*/
public Node middle() {
if (head == null) {
return null;
}
Node firstCounter = head;
Node secondCounter = firstCounter.next;
while (secondCounter != null && secondCounter.next != null) {
firstCounter = firstCounter.next;
secondCounter = secondCounter.next.next;
}
return firstCounter;
}
/**
* Swaps nodes of two given values a and b.
*