Skip to content

Instantly share code, notes, and snippets.

@apremalal
Last active December 13, 2015 21:08
Show Gist options
  • Save apremalal/4975361 to your computer and use it in GitHub Desktop.
Save apremalal/4975361 to your computer and use it in GitHub Desktop.
An implementation of single Linked List in java.
import java.util.Arrays;
public class SingleLinkedList {
Node head;
int i = 0;
void insert(SingleLinkedList li, Node x) {
x.next = li.head;
li.head = x;
}
Node search(SingleLinkedList li, int key) {
Node x = li.head;
while (x != null && x.key != key) {
x = x.next;
}
return x;
}
}
class Node {
Node next;
int key;
Node(int key) {
this.key = key;
}
}
//Testing the algorithm
class Test {
public static void main(String args[]) {
SingleLinkedList sl = new SingleLinkedList();
sl.insert(sl, new Node(4));
sl.insert(sl, new Node(3));
sl.insert(sl, new Node(9));
sl.insert(sl, new Node(6));
System.out.println(sl.search(sl, 20).key);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment