Java Single LinkedList
1. insertNode
2. insertFront
3. insertLast
4. removeLast
5. removeFront
6. printNode
See File:
$j/JavaSingleLinkedList.java
public static void printNode(Node head){
Node curr = head;
while(curr != null){
pl(curr.toString());
curr = curr.next;
}
}
public static Node insertNode(Node head, Node node){
if(head == null){
head = node;
}else{
Node curr = head;
Node prev = null;
while(curr.next != null){
curr = curr.next;
}
curr.next = node;
}
return head;
}
public static Node insertFront(Node head, Node node){
if(head == null){
head = node;
}else{
node.next = head;
head = node;
}
return head;
}
public static Node insertLast(Node head, Node node){
if(head == null){
head = node;
}else{
Node curr = head;
while(curr.next != null){
curr = curr.next;
}
curr.next = node;
}
return head;
}
public static Node removeFront(Node head){
if(head == null){
// Nothing to remove
}else{
Node h2 = head.next;
head = h2;
}
return head;
}
public static Node removeLast(Node head){
if(head == null){
// Nothing to remove
}else{
Node curr = head;
Node prev = null;
while(curr.next != null){
prev = curr;
curr = curr.next;
}
if(prev == null){ // Only one node
head = null;
}else{
prev.next = null;
}
}
return head;
}