Linked list in Java - comparing two lists -
i need create recursive method in java checks 2 char lists. if second list includes chars in first list @ least once , in same order should return true, else should return false. example:
list 1: "abbcd"(every char in node), list 2: "abbcccddd"(every char in node) should return true.
example 2: "abbcd", list 2: "abcd" should return false.
i have ideas can't reach definite solution. ideas anyone?
i'll assume use usual node structure data element , reference next node. 1 can define function follows:
contains(null, haystack) = true (since every string contains empty string)
contains(pattern, null) = false (since empty string doesn't contain patterns)
contains(pattern, haystack) = contains(pattern.next, haystack.next) if pattern.data = haystack.data (we found match , proceed next item in both lists)
contains(pattern, haystack) = contains(pattern.next, haystack.next) else (we found no match , try next character in haystack)
Comments
Post a Comment