Skip to content

Instantly share code, notes, and snippets.

@astkaasa
Created March 18, 2013 01:06
Show Gist options
  • Save astkaasa/5184376 to your computer and use it in GitHub Desktop.
Save astkaasa/5184376 to your computer and use it in GitHub Desktop.
public class DeleteMiddleNode {
public static void main( String[] args ) {
LinkNode a = new LinkNode ( 5 );
LinkNode b = new LinkNode ( 10 );
LinkNode c = new LinkNode ( 9 );
LinkNode d = new LinkNode ( 4 );
LinkNode e = new LinkNode ( 7 );
a.setNext( b );
b.setNext( c );
c.setNext( d );
d.setNext( e );
printLinkedList( a );
DeleteNode( c );
printLinkedList( a );
}
public static void printLinkedList( LinkNode head ) {
while ( head != null ) {
System.out.print( head.getValue() );
System.out.print( "->" );
head = head.getNext();
}
System.out.println( "null" );
}
public static void DeleteNode( LinkNode n ) {
n.setValue( n.getNext().getValue() );
n.setNext( n.getNext().getNext() );
}
}
public class LinkNode {
private int value;
private LinkNode next;
public LinkNode( int val ) {
this.value = val;
this.next = null;
}
public int getValue() {
return value;
}
public void setValue( int value ) {
this.value = value;
}
public LinkNode getNext() {
return next;
}
public void setNext( LinkNode n ) {
this.next = n;
}
public void append( LinkNode n ) {
LinkNode currNode = this;
while ( currNode.next != null ) {
currNode = currNode.next;
}
currNode.next = n;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment