Skip to content

Instantly share code, notes, and snippets.

@sdmg15
Created September 27, 2017 19:52
Show Gist options
  • Save sdmg15/4f327a76af0a2ac1ad043beebb385b94 to your computer and use it in GitHub Desktop.
Save sdmg15/4f327a76af0a2ac1ad043beebb385b94 to your computer and use it in GitHub Desktop.
public class LL {
public LL(){}
static class Vertex{
int data;
Vertex next;
public Vertex(int data){
this.data = data;
this.next = null;
}
}
Vertex head = null;
int size;
//Insert a node at the begining of the list;
public void push(int data){
if(head == null) head = new Vertex(data);
else{
Vertex vertex = new Vertex(data);
vertex.next = head;
head = vertex;
}
size++;
}
public void insertAfter(int i,int data){
if( i <= size) {
Vertex tmp = head;
Vertex prev = null;
for (int j = 0; j < i; j++) {
prev = tmp;
tmp = tmp.next;
}
Vertex v = new Vertex(data);
v.next = tmp;
prev.next = v;
}
}
public void printList(){
Vertex vertex = head;
while(vertex != null){
System.out.println(vertex.data +" ");
vertex = vertex.next;
}
}
public void deleteNode(int i){
if(head == null) return;
else{
if( i < size){
Vertex toDel = getI(i);
Vertex before= getI(i-1);
before.next = toDel.next;
}else return;
}
}
public Vertex getI(int i ){
Vertex tm = head;
for(int j = 0; j < i; ++j){
tm = tm.next;
}
return tm;
}
public int deleteNode(Object data){
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment