Created
March 7, 2017 04:53
-
-
Save khayyamsaleem/b440402c844826a304d14c0440554caa to your computer and use it in GitHub Desktop.
nick polich is a weenie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test{ | |
private static class Node<E extends Comparable<E>>{ | |
E data; | |
Node<E> next; | |
public Node(E data){ | |
this.data = data; | |
this.next = null; | |
} | |
} | |
public static boolean find(int a, Node<Integer> x){ | |
Node<Integer> t = x; | |
while(t != null){ | |
if(t.data.compareTo(a) == 0) | |
return true; | |
t = t.next; | |
} | |
return false; | |
} | |
public static boolean nonDuplicates(Node<Integer> x){ | |
Node<Integer> dup = new Node<>(x.data); | |
Node<Integer> dupt = dup; | |
Node<Integer> t = x.next; | |
while(t != null){ | |
if(find(t.data, dup)){ | |
return true; | |
} | |
dupt.next = new Node<>(t.data); | |
t = t.next; | |
dupt = dupt.next; | |
} | |
return false; | |
} | |
public static void main(String[] args){ | |
Node<Integer> x = new Node<Integer>(1); | |
x.next = new Node<>(2); | |
x.next.next = new Node<>(3); | |
x.next.next.next = new Node<>(5); | |
x.next.next.next.next = new Node<>(9); | |
Node<Integer> y = new Node<>(9); | |
y.next = new Node<>(2); | |
y.next.next = new Node<>(3); | |
y.next.next.next = new Node<>(1); | |
y.next.next.next.next = new Node<>(9); | |
System.out.println(nonDuplicates(x)); | |
System.out.println(nonDuplicates(y)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment