Interview Question

Interview Seattle, WA

Given two linked lists A and B, return a new linked list C

  , where C consists of all elements in A or B that are contained in only A or only B.
Tags:
data structures, programming
Answer

Interview Answer

2 Answers

1

Hint: hash table!

Interview Candidate on Mar 19, 2009
0

public static void createUniqueListC(List<Integer> listA, List<Integer> listB) { Set<Integer> set = null; List<Integer> list = null; if (listA != null && listB != null) { set = new LinkedHashSet<Integer>(); list = listA; int length = listA.size() + listB.size(); for (int i=0; i<length; i++) { if (i>=listA.size()) { list = listB; i = 0; length = length - listA.size(); } if (!set.add(list.get(i))) { set.remove(list.get(i)); } } Iterator<Integer> itr = set.iterator(); while (itr.hasNext()) { System.out.println(itr.next()+" "); } } }

Rajiv on Aug 23, 2010

Add Answers or Comments

To comment on this, Sign In or Sign Up.