Merge pull request #1476 from shellhub/dev

Updated SinglyLinkedList
This commit is contained in:
Du Yuanchao 2020-09-22 09:56:54 +08:00 committed by GitHub
commit c7f605ef7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 237 additions and 114 deletions

View File

@ -65,6 +65,7 @@
* [ListAddnFun](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/ListAddnFun.java) * [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) * [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) * [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) * [SinglyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/SinglyLinkedList.java)
* Queues * Queues
* [GenericArrayListQueue](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Queues/GenericArrayListQueue.java) * [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) * [Problem04](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem04.java)
* [Problem06](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem06.java) * [Problem06](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem06.java)
* [Problem07](https://github.com/TheAlgorithms/Java/blob/master/ProjectEuler/Problem07.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 ## Searches
* [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/BinarySearch.java) * [BinarySearch](https://github.com/TheAlgorithms/Java/blob/master/Searches/BinarySearch.java)

View File

@ -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);
}
}

View File

@ -18,15 +18,15 @@ public class SinglyLinkedList {
private Node head; private Node head;
/** /**
* size of SinglyLinkedList * Size of SinglyLinkedList
*/ */
private int size; private int size;
/** /**
* init SinglyLinkedList * Init SinglyLinkedList
*/ */
public SinglyLinkedList() { public SinglyLinkedList() {
head = new Node(0); head = null;
size = 0; 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) { public void insertHead(int x) {
insertNth(x, 0); 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) { public void insert(int data) {
insertNth(data, size); 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 data data to be stored in a new node
* @param position position at which a new node is to be inserted * @param position position at which a new node is to be inserted
*/ */
public void insertNth(int data, int position) { public void insertNth(int data, int position) {
checkBounds(position, 0, size); 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); 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; newNode.next = cur.next;
cur.next = newNode; cur.next = newNode;
size++; size++;
} }
/** /**
* This method deletes an element at the head * Deletes a node at the head
*
* @return The element deleted
*/ */
public void deleteHead() { public void deleteHead() {
deleteNth(0); deleteNth(0);
} }
/** /**
* This method deletes an element at the tail * Deletes an element at the tail
*/ */
public void delete() { public void delete() {
deleteNth(size - 1); deleteNth(size - 1);
} }
/** /**
* This method deletes an element at Nth position * Deletes an element at Nth position
*/ */
public void deleteNth(int position) { public void deleteNth(int position) {
checkBounds(position, 0, size - 1); 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; Node cur = head;
for (int i = 0; i < position; ++i) { for (int i = 0; i < position - 1; ++i) {
cur = cur.next; cur = cur.next;
} }
//Node destroy = cur.next; Node destroy = cur.next;
cur.next = cur.next.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--; size--;
} }
@ -140,47 +139,68 @@ public class SinglyLinkedList {
} }
/** /**
* clear all nodes in list * Clear all nodes in the list
*/ */
public void clear() { public void clear() {
if (size == 0) { Node cur = head;
return;
}
Node prev = head.next;
Node cur = prev.next;
while (cur != null) { while (cur != null) {
prev = null; // clear to let GC do its work Node prev = cur;
prev = cur;
cur = cur.next; cur = cur.next;
prev = null; // clear to let GC do its work
} }
prev = null; head = null;
head.next = null;
size = 0; size = 0;
} }
/** /**
* Checks if the list is empty * 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() { public boolean isEmpty() {
return size == 0; 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() { public int size() {
return 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 @Override
public String toString() { public String toString() {
if (size == 0) { if (size == 0) {
return ""; return "";
} }
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
Node cur = head.next; Node cur = head;
while (cur != null) { while (cur != null) {
builder.append(cur.value).append("->"); builder.append(cur.value).append("->");
cur = cur.next; cur = cur.next;
@ -188,79 +208,43 @@ public class SinglyLinkedList {
return builder.replace(builder.length() - 2, builder.length(), "").toString(); 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 * Driver Code
*
* @param args Command line arguments
*/ */
public static void main(String args[]) { public static void main(String[] arg) {
SinglyLinkedList myList = new SinglyLinkedList(); SinglyLinkedList list = new SinglyLinkedList();
assert myList.isEmpty(); assert list.isEmpty();
assert myList.toString().equals(""); assert list.size() == 0
&& list.count() == 0;
assert list.toString().equals("");
myList.insertHead(5); /* Test insert function */
myList.insertHead(7); list.insertHead(5);
myList.insertHead(10); list.insertHead(7);
assert myList.toString().equals("10->7->5"); list.insertHead(10);
list.insert(3);
list.insertNth(1, 4);
assert list.toString().equals("10->7->5->3->1");
myList.deleteHead(); /* Test delete function */
assert myList.toString().equals("7->5"); list.deleteHead();
list.deleteNth(1);
list.delete();
assert list.toString().equals("7->3");
myList.insertNth(11, 2); assert list.size == 2
assert myList.toString().equals("7->5->11"); && list.size() == list.count();
myList.deleteNth(1); list.clear();
assert myList.toString().equals("7->11"); assert list.isEmpty();
myList.clear(); try {
assert myList.isEmpty(); list.delete();
assert false; /* this should not happen */
/* Test MergeTwoSortedLinkedList */ } catch (Exception e) {
SinglyLinkedList listA = new SinglyLinkedList(); assert true; /* this should happen */
SinglyLinkedList listB = new SinglyLinkedList();
for (int i = 10; i >= 2; i -= 2) {
listA.insertSorted(i);
listB.insertSorted(i - 1);
} }
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");
} }
} }

View File

@ -0,0 +1,30 @@
package ProjectEuler;
/**
* A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
* <p>
* a^2 + b^2 = c^2
* For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
* <p>
* There exists exactly one Pythagorean triplet for which a + b + c = 1000.
* Find the product abc.
* <p>
* 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 */
}
}

View File

@ -0,0 +1,55 @@
package ProjectEuler;
/**
* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
* <p>
* Find the sum of all the primes below two million.
* <p>
* 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;
}
}