Amazon Interview Question: Given two linked lists A and ... | Glassdoor

Interview Question

Software Design Engineer 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 listA, List listB) {
        Set set = null;
        List list = null;
        if (listA != null && listB != null) {
            set = new LinkedHashSet();
            list = listA;
            int length = listA.size() + listB.size();
            for (int i=0; i=listA.size()) {
                    list = listB;
                    i = 0;
                    length = length - listA.size();
                }

                if (!set.add(list.get(i))) {
                    set.remove(list.get(i));
                }
            }
            Iterator 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.