diff --git a/DIRECTORY.md b/DIRECTORY.md index 45161e5f..ab43fd87 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -65,6 +65,7 @@ * [ListAddnFun](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/ListAddnFun.java) * [Merge K SortedLinkedlist](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/Merge_K_SortedLinkedlist.java) * [MergeSortedArrayList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/MergeSortedArrayList.java) + * [MergeSortedSinglyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/MergeSortedSinglyLinkedList.java) * [SinglyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/SinglyLinkedList.java) * Queues * [GenericArrayListQueue](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Queues/GenericArrayListQueue.java) @@ -207,6 +208,8 @@ * [Problem04](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem04.java) * [Problem06](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem06.java) * [Problem07](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem07.java) + * [Problem09](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem09.java) + * [Problem10](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem10.java) ## Searches * [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/BinarySearch.java) diff --git a/DataStructures/Lists/MergeSortedSinglyLinkedList.java b/DataStructures/Lists/MergeSortedSinglyLinkedList.java new file mode 100644 index 00000000..2daae0d7 --- /dev/null +++ b/DataStructures/Lists/MergeSortedSinglyLinkedList.java @@ -0,0 +1,51 @@ +package DataStructures.Lists; + +public class MergeSortedSinglyLinkedList extends SinglyLinkedList { + + public static void main(String[] args) { + SinglyLinkedList listA = new SinglyLinkedList(); + SinglyLinkedList listB = new SinglyLinkedList(); + + for (int i = 2; i <= 10; i += 2) { + listA.insert(i); + listB.insert(i - 1); + } + assert listA.toString().equals("2->4->6->8->10"); + assert listB.toString().equals("1->3->5->7->9"); + assert merge(listA, listB).toString().equals("1->2->3->4->5->6->7->8->9->10"); + } + + /** + * Merge two sorted SingleLinkedList + * + * @param listA the first sorted list + * @param listB the second sored list + * @return merged sorted list + */ + public static SinglyLinkedList merge(SinglyLinkedList listA, SinglyLinkedList listB) { + Node headA = listA.getHead(); + Node headB = listB.getHead(); + + int size = listA.size() + listB.size(); + + Node head = new Node(); + Node tail = head; + while (headA != null && headB != null) { + if (headA.value <= headB.value) { + tail.next = headA; + headA = headA.next; + } else { + tail.next = headB; + headB = headB.next; + } + tail = tail.next; + } + if (headA == null) { + tail.next = headB; + } + if (headB == null) { + tail.next = headA; + } + return new SinglyLinkedList(head.next, size); + } +} diff --git a/DataStructures/Lists/SinglyLinkedList.java b/DataStructures/Lists/SinglyLinkedList.java index 5fda2f5f..7c10f8cf 100644 --- a/DataStructures/Lists/SinglyLinkedList.java +++ b/DataStructures/Lists/SinglyLinkedList.java @@ -18,15 +18,15 @@ public class SinglyLinkedList { private Node head; /** - * size of SinglyLinkedList + * Size of SinglyLinkedList */ private int size; /** - * init SinglyLinkedList + * Init SinglyLinkedList */ public SinglyLinkedList() { - head = new Node(0); + head = null; size = 0; } @@ -42,87 +42,86 @@ public class SinglyLinkedList { } /** - * This method inserts an element at the head + * Inserts an element at the head of the list * - * @param x Element to be added + * @param x element to be added */ public void insertHead(int x) { insertNth(x, 0); } /** - * insert an element at the tail of list + * Insert an element at the tail of the list * - * @param data Element to be added + * @param data element to be added */ public void insert(int data) { insertNth(data, size); } /** - * Inserts a new node at a specified position + * Inserts a new node at a specified position of the list * * @param data data to be stored in a new node * @param position position at which a new node is to be inserted */ public void insertNth(int data, int position) { checkBounds(position, 0, size); - Node cur = head; - for (int i = 0; i < position; ++i) { - cur = cur.next; - } - Node node = new Node(data); - node.next = cur.next; - cur.next = node; - size++; - } - - /** - * Insert element to list, always sorted - * - * @param data to be inserted - */ - public void insertSorted(int data) { - Node cur = head; - while (cur.next != null && data > cur.next.value) { - cur = cur.next; - } - Node newNode = new Node(data); + if (head == null) { /* the list is empty */ + head = newNode; + size++; + return; + } else if (position == 0) { /* insert at the head of the list */ + newNode.next = head; + head = newNode; + size++; + return; + } + Node cur = head; + for (int i = 0; i < position - 1; ++i) { + cur = cur.next; + } newNode.next = cur.next; cur.next = newNode; size++; } + /** - * This method deletes an element at the head - * - * @return The element deleted + * Deletes a node at the head */ public void deleteHead() { deleteNth(0); } /** - * This method deletes an element at the tail + * Deletes an element at the tail */ public void delete() { deleteNth(size - 1); } /** - * This method deletes an element at Nth position + * Deletes an element at Nth position */ public void deleteNth(int position) { checkBounds(position, 0, size - 1); + if (position == 0) { + Node destroy = head; + head = head.next; + destroy = null; /* clear to let GC do its work */ + size--; + return; + } Node cur = head; - for (int i = 0; i < position; ++i) { + for (int i = 0; i < position - 1; ++i) { cur = cur.next; } - //Node destroy = cur.next; + Node destroy = cur.next; cur.next = cur.next.next; - //destroy = null; // clear to let GC do its work + destroy = null; // clear to let GC do its work size--; } @@ -140,47 +139,68 @@ public class SinglyLinkedList { } /** - * clear all nodes in list + * Clear all nodes in the list */ public void clear() { - if (size == 0) { - return; - } - Node prev = head.next; - Node cur = prev.next; + Node cur = head; while (cur != null) { - prev = null; // clear to let GC do its work - prev = cur; + Node prev = cur; cur = cur.next; + prev = null; // clear to let GC do its work } - prev = null; - head.next = null; + head = null; size = 0; } /** * Checks if the list is empty * - * @return true is list is empty + * @return {@code true} if list is empty, otherwise {@code false}. */ public boolean isEmpty() { return size == 0; } /** - * Returns the size of the linked list + * Returns the size of the linked list. + * + * @return the size of the list. */ public int size() { return size; } + /** + * Get head of the list. + * + * @return head of the list. + */ + public Node getHead() { + return head; + } + + /** + * Calculate the count of the list manually + * + * @return count of the list + */ + public int count() { + int count = 0; + Node cur = head; + while (cur != null) { + cur = cur.next; + count++; + } + return count; + } + @Override public String toString() { if (size == 0) { return ""; } StringBuilder builder = new StringBuilder(); - Node cur = head.next; + Node cur = head; while (cur != null) { builder.append(cur.value).append("->"); cur = cur.next; @@ -188,79 +208,43 @@ public class SinglyLinkedList { return builder.replace(builder.length() - 2, builder.length(), "").toString(); } - /** - * Merge two sorted SingleLinkedList - * - * @param listA the first sorted list - * @param listB the second sored list - * @return merged sorted list - */ - public static SinglyLinkedList merge(SinglyLinkedList listA, SinglyLinkedList listB) { - Node headA = listA.head.next; - Node headB = listB.head.next; - - int size = listA.size() + listB.size(); - - Node head = new Node(); - Node tail = head; - while (headA != null && headB != null) { - if (headA.value <= headB.value) { - tail.next = headA; - headA = headA.next; - } else { - tail.next = headB; - headB = headB.next; - } - tail = tail.next; - } - if (headA == null) { - tail.next = headB; - } - if (headB == null) { - tail.next = headA; - } - return new SinglyLinkedList(head, size); - } /** - * Main method - * - * @param args Command line arguments + * Driver Code */ - public static void main(String args[]) { - SinglyLinkedList myList = new SinglyLinkedList(); - assert myList.isEmpty(); - assert myList.toString().equals(""); + public static void main(String[] arg) { + SinglyLinkedList list = new SinglyLinkedList(); + assert list.isEmpty(); + assert list.size() == 0 + && list.count() == 0; + assert list.toString().equals(""); - myList.insertHead(5); - myList.insertHead(7); - myList.insertHead(10); - assert myList.toString().equals("10->7->5"); + /* Test insert function */ + list.insertHead(5); + list.insertHead(7); + list.insertHead(10); + list.insert(3); + list.insertNth(1, 4); + assert list.toString().equals("10->7->5->3->1"); - myList.deleteHead(); - assert myList.toString().equals("7->5"); + /* Test delete function */ + list.deleteHead(); + list.deleteNth(1); + list.delete(); + assert list.toString().equals("7->3"); - myList.insertNth(11, 2); - assert myList.toString().equals("7->5->11"); + assert list.size == 2 + && list.size() == list.count(); - myList.deleteNth(1); - assert myList.toString().equals("7->11"); + list.clear(); + assert list.isEmpty(); - myList.clear(); - assert myList.isEmpty(); - - /* Test MergeTwoSortedLinkedList */ - SinglyLinkedList listA = new SinglyLinkedList(); - SinglyLinkedList listB = new SinglyLinkedList(); - - for (int i = 10; i >= 2; i -= 2) { - listA.insertSorted(i); - listB.insertSorted(i - 1); + try { + list.delete(); + assert false; /* this should not happen */ + } catch (Exception e) { + assert true; /* this should happen */ } - assert listA.toString().equals("2->4->6->8->10"); - assert listB.toString().equals("1->3->5->7->9"); - assert SinglyLinkedList.merge(listA, listB).toString().equals("1->2->3->4->5->6->7->8->9->10"); - } } diff --git a/ProjectEuler/Problem09.java b/ProjectEuler/Problem09.java new file mode 100644 index 00000000..5d259d50 --- /dev/null +++ b/ProjectEuler/Problem09.java @@ -0,0 +1,30 @@ +package ProjectEuler; + +/** + * A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, + *
+ * a^2 + b^2 = c^2 + * For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. + *
+ * There exists exactly one Pythagorean triplet for which a + b + c = 1000. + * Find the product abc. + *
+ * link: https://projecteuler.net/problem=9 + */ +public class Problem09 { + public static void main(String[] args) { + assert solution1() == 31875000; + } + + private static int solution1() { + for (int i = 0; i <= 300; ++i) { + for (int j = 0; j <= 400; ++j) { + int k = 1000 - i - j; + if (i * i + j * j == k * k) { + return i * j * k; + } + } + } + return -1; /* should not happen */ + } +} \ No newline at end of file diff --git a/ProjectEuler/Problem10.java b/ProjectEuler/Problem10.java new file mode 100644 index 00000000..451948c5 --- /dev/null +++ b/ProjectEuler/Problem10.java @@ -0,0 +1,55 @@ +package ProjectEuler; + +/** + * The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. + *
+ * Find the sum of all the primes below two million. + *
+ * link: https://projecteuler.net/problem=10 + */ +public class Problem10 { + public static void main(String[] args) { + long[][] testNumbers = { + {2000000, 142913828922L}, + {10000, 5736396}, + {5000, 1548136}, + {1000, 76127}, + {10, 17}, + {7, 10} + }; + + for (long[] testNumber : testNumbers) { + assert solution1(testNumber[0]) == testNumber[1]; + } + } + + /*** + * Checks if a number is prime or not + * @param n the number + * @return {@code true} if {@code n} is prime + */ + private static boolean isPrime(int n) { + if (n == 2) { + return true; + } + if (n < 2 || n % 2 == 0) { + return false; + } + for (int i = 3, limit = (int) Math.sqrt(n); i <= limit; i += 2) { + if (n % i == 0) { + return false; + } + } + return true; + } + + private static long solution1(long n) { + long sum = 0; + for (int i = 2; i < n; ++i) { + if (isPrime(i)) { + sum += i; + } + } + return sum; + } +} \ No newline at end of file