Skip to content

Instantly share code, notes, and snippets.

@jyhjuzi
Created June 27, 2014 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jyhjuzi/3cf87cd4799352c6e4d9 to your computer and use it in GitHub Desktop.
Save jyhjuzi/3cf87cd4799352c6e4d9 to your computer and use it in GitHub Desktop.
public class Q2_3{
public static void main(String args[]){
Node test = arrayToLinkedList(new int[] {1,2,3,4,5}) ;
deleteElement(test.next.next.next);
System.out.println(compareLinkedList(test,
arrayToLinkedList(new int[] { 1,2,3,5})));
}
public static void deleteElement(Node n){
n.value = n.next.value;
n.next = n.next.next;
}
private static Node arrayToLinkedList(int[] array) {
Node head = new Node(array[0]);
Node current = head;
for (int i = 1; i < array.length; i++) {
current.next = new Node(array[i]);
current = current.next;
}
return head;
}
private static boolean compareLinkedList(Node root1, Node root2) {
Node a = root1;
Node b = root2;
do {
if (root1 == null && root2 == null)
return true;
if (root1 == null || root2 == null)
return false;
if (root1.value != root2.value)
return false;
root1 = root1.next;
root2 = root2.next;
} while (true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment