Skip to content

upate SinglyLinkedList #1128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 88 additions & 12 deletions DataStructures/Lists/SinglyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public SinglyLinkedList() {
size = 0;
}

/**
* Init SinglyLinkedList with specified head node and size
*
* @param head the head node of list
* @param size the size of list
*/
public SinglyLinkedList(Node head, int size) {
this.head = head;
this.size = size;
}

/**
* This method inserts an element at the head
*
Expand Down Expand Up @@ -66,6 +77,23 @@ public void insertNth(int data, int position) {
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);
newNode.next = cur.next;
cur.next = newNode;
size++;
}

/**
* This method deletes an element at the head
*
Expand Down Expand Up @@ -142,7 +170,7 @@ public boolean isEmpty() {
/**
* Returns the size of the linked list
*/
public int getSize() {
public int size() {
return size;
}

Expand All @@ -160,38 +188,78 @@ public String 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
*
* @param args Command line arguments
*/
public static void main(String args[]) {
SinglyLinkedList myList = new SinglyLinkedList();

assert myList.isEmpty();
assert myList.toString().equals("");

myList.insertHead(5);
myList.insertHead(7);
myList.insertHead(10);

System.out.println(myList);; // 10 -> 7 -> 5
assert myList.toString().equals("10->7->5");

myList.deleteHead();

System.out.println(myList);; // 7 -> 5
assert myList.toString().equals("7->5");

myList.insertNth(11, 2);

System.out.println(myList);; // 7 -> 5 -> 11
assert myList.toString().equals("7->5->11");

myList.deleteNth(1);

System.out.println(myList);; // 7-> 11
assert myList.toString().equals("7->11");

myList.clear();
assert myList.isEmpty();

System.out.println(myList); // ""
/* 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);
}
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");

}
}
Expand All @@ -212,13 +280,21 @@ class Node {
*/
Node next;

Node() {

}

/**
* Constructor
*
* @param value Value to be put in the node
*/
Node(int value) {
this(value, null);
}

Node(int value, Node next) {
this.value = value;
this.next = null;
this.next = next;
}
}