Skip to content

Instantly share code, notes, and snippets.

@mchayapol
Created September 16, 2015 03:09
Show Gist options
  • Save mchayapol/a11a5fcb2e7973249bd6 to your computer and use it in GitHub Desktop.
Save mchayapol/a11a5fcb2e7973249bd6 to your computer and use it in GitHub Desktop.
Linked List Skeleton 01
package edu.au.scitech.sc2101;
public class IntegerLinkedList {
Node head;
public class Node {
int value;
Node next;
// Constructor for this node
public Node(int value) {
this.value = value;
}
}
// Constructor for this list
public IntegerLinkedList() {
head = null;
}
public void add(int newValue) {
// create a new node with newValue
Node n = new Node(newValue);
n.next = null;
// Update the head
head = n;
}
public String toString() {
StringBuffer sb = new StringBuffer();
// return content of the list as string
sb.append("[");
for(Node currentNode = head;
currentNode != null;
currentNode = currentNode.next)
{
sb.append( currentNode.value );
sb.append(",");
}
sb.append("]");
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment