This question only has weight in it if the values in each list are sorted. If that's the case, then this will find duplicates recursively (in pseudocode) Node merge(Node n1, Node n2) { IF n1 == null OR n2 == null RETURN null ELSE IF n1. Value == n2.
Value Node dupNode(n1. Value); dupNode. Next = merge(n1.
Next, n2. Next); RETURN dupNode; ELSE IF n1. Value Value RETURN merge(n1.
Next, n2) ELSE RETURN merge(n1, n2. Next) } Given a list of length L1 and L2 this will merge them in O(L1 + L2) It does so non-destructively, by creating new nodes for the duplicates. You can easily modify it to "steal" from one of the lists if you wish.
This question only has weight in it if the values in each list are sorted. If that's the case, then this will find duplicates recursively (in pseudocode) Node merge(Node n1, Node n2) { IF n1 == null OR n2 == null RETURN null ELSE IF n1. Value == n2.
Value Node dupNode(n1. Value); dupNode. Next = merge(n1.
Next, n2. Next); RETURN dupNode; ELSE IF n1. Value Value RETURN merge(n1.
Next, n2) ELSE RETURN merge(n1, n2. Next) } Given a list of length L1 and L2, this will merge them in O(L1 + L2). It does so non-destructively, by creating new nodes for the duplicates.
You can easily modify it to "steal" from one of the lists if you wish.
This problem depends on the constraints. The simplest, most naive solution is if you have two elements of size n, you iterate over one list and compare it to every item in the second list. Solution: O(n2) But of course you can do much better.
Now, if you have a HashSet (or other near-O(1)) data structure available then this is what you can do: Iterate over one list. Add each element to the set. Iterate over the second list.
If the element is in the set then add it to the result list. Solution: O(n).
If you do not care about duplicates using Set's built-in retainAll() method is an easy solution. List list1 = ...; // The smaller list List list2 = ...; ... final Set s1 = new HashSet(list1); s1. RetainAll(list2); // Try s1.
RetainAll(new HashSet(list2)); if the lists are really bug final List solution = new LinkedList(s1).
Must use recursion. I am not sure how retainAll works, but even if it did use recursion internally, I doubt it would be suitable for the purpose of this assignment. – Cambium Apr 26 '10 at 2:21.
There are many ways to interpret the question. Are we looking for an intersection of the sets represented by the lists, or are we looking for a longest common subsequence? Are the lists always sorted?
In my recursive solution, I assume that we are looking for some longest subsequence, and I don't assume anything about the items order: private static List longestCommonSubseq(List a, int indA, List b, int indB){ if (indA == a.size() || indB == b.size()) return Collections.emptyList(); T itemA = a. Get(indA); T itemB = b. Get(indB); List res; if (itemA.
Equals(itemB)){ res = new ArrayList(); res. Add(itemA); res. AddAll(longestCommonSubseq(a, indA+1, b, indB+1)); }else{ List opt1 = longestCommonSubseq(a, indA+1, b, indB); List opt2 = longestCommonSubseq(a, indA, b, indB+1); if (opt1.size()>opt2.size()) res = opt1; else res = opt2; } return res; } public static List longestCommonSubseq(List a, List b){ return longestCommonSubseq(a,0,b,0); } Note: For simplicity, in my solutions the lists should be random access (e.g. ArrayList).
Ok, I'm not making any assumptions about what you want beyond what you asked for. Below is a recursive function which finds common elements of two linked lists. It takes O(n^2) time, what that what you get with your setup.
Note that while this is tail-recursive, Java (generally) doesn't optimize that so this will blow the stack for long lists. Import java.util. *; public class CommonNodeLinkedList { public static void main(String args) { List list1_items = Arrays.
AsList(2, 5, 7, 10); List list2_items = Arrays. AsList(2, 4, 8, 10); LinkedList list1 = new LinkedList(); list1. AddAll(list1_items); LinkedList list2 = new LinkedList(); list2.
AddAll(list2_items); System.out. Println("List 1 : " + list1); System.out. Println("List 2 : " + list2); System.out.
Println("Common Nodes: " + findCommonNodes(list1, list2)); } public static LinkedList findCommonNodes(LinkedList list1, LinkedList list2) { return findCommonNodes_helper(list1, list2, new LinkedList()); } public static LinkedList findCommonNodes_helper(LinkedList list1, LinkedList list2, LinkedList result) { if (list1.isEmpty()) return result; Integer head = list1.pop(); if (list2. Contains(head)) { result. Add(head); } return findCommonNodes_helper(list1, list2, result); } }.
Please check the below link for answer: datastructurequestions.blogspot.com/2010....
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.