shaktiman
BAN USER- 0of 0 votes
AnswersWrite a program to calculate Sum of two singly linked lists.
- shaktiman in -
e.g.
1-->2-->3
8->9->10
.
Result list should be 10-->2-->3
You are not allowed to make any change in input lists. Those are read only.| Report Duplicate | Flag | PURGE
Microsoft Software Engineer in Test Algorithm
final static char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
public static void main(String[] args) {
int max = 26 * 26 * 26 + 26 * 26 + 26;
for (int i = 1; i <= max; i++) {
System.out.print(findNthNumber(i) + " ");
if (i % 26 == 0) {
System.out.println();
}
if (i == 26 * 26 + 26) {
System.out.println();
}
}
}
static String findNthNumber(int n) {
StringBuilder builder = new StringBuilder();
while (n > 0) {
int index = n % 26;
if (index == 0) {
index = chars.length;
n = n - 1;
}
builder.append(chars[index - 1]);
n = n / 26;
}
return builder.reverse().toString();
}
public class SubSequence {
public static void main(String[] args) {
findLongestSubsequence("aabbaabbaabbaabababa");
findLongestSubsequence("aabbcbbbadef");
findLongestSubsequence("aabaadddddaa");
findLongestSubsequence("aabaacdddddaa");
findLongestSubsequence("bbbcccbbbbcccca");
}
static void findLongestSubsequence(String input) {
// make validations for null and length 1 strings here
int initial = 0, changed = 0;
char firstChar = input.charAt(0), secondChar = input.charAt(1);
firstChar = input.charAt(0);
String subSequence = "";
for (int i = 0; i < input.length() - 1; i++) {
char current = input.charAt(i);
char next = input.charAt(i + 1);
// It may be xyx or xyz
if (current != next) {
//End of current sequence
if(next != firstChar && next != secondChar){
String currentSequence = input.substring(initial,i+1);
if(currentSequence.length() > subSequence.length()){
subSequence = currentSequence;
}
//Re initialize the sequence
initial = changed;
firstChar = current;
secondChar = next;
}
//From next character we can restart
changed = i+1;
}
}
//If subsequence ends at last
if(changed <= input.length()-1){
String currentSequence = input.substring(initial,input.length());
if(currentSequence.length() > subSequence.length()){
subSequence = currentSequence;
}
}
System.out.println(subSequence);
}
}
static void sumList(Node head1, Node head2, Node result) {
if (head1 == null || head2 == null) {
return;
}
sumList(head1.next, head2.next, result.next);
int carry = 0;
Node next = result.next;
if (next != null && next.data > 10) {
carry = next.data / 10;
next.data = next.data % 10;
}
result.data = head1.data + head2.data + carry;
}
RepDo you need dua for controlling husband? Contact Guru ji right now. He provides the best and simple dua to ...
o(n^2) solution
- shaktiman June 13, 2013