sankarmail
BAN USERThe above one is for BST
- sankarmail February 05, 2012public void buildDLstWrapper( Node root)
{
Node prev = null;
buildDLst(root,prev);
}
public void buildDLst( Node root, Node prev)
{
if(root!=null)
{
buildDLst(root.left,prev);
if(prev!=null)
prev.right = root;
root.left = prev;
prev = root;
buildDLst(root.right, prev);
}
}
This is good, but we can name reverse function with different name.
- sankarmail January 12, 2012I used reverse operation repeatedly, and got the expected output. For example
1234abcd, take 234abc, do the following steps,
cba432 //reverse
abc432 // reverse upto middle end
abc234
Do the same operation for the next set bc23,
32cb,
23cb
23bc
next set of string 3b
b3,
final output is 1a2b3c4d, not sure about complexity, but everything in place
void reverse(char str[],int start, int end)
{
int temp;
while(start<end)
{
temp=str[start];
str[start++] = str[end];
str[end--] = temp;
}
}
void func(char str[])
{
int end = strlen(str)-2;
int start = 1;
while(start<end)
{
reverse(str,start,end);
reverse(str,start,start+((end-start)/2));
reverse(str,1+start+((end-start)/2),end);
start++;
end--;
printf("%s\n",str);
}
}
This is based on a problem in CLRS. Read heap chapter, there is a problem that talks about how to sort k sorted lists. This problem is just extension of that.
Here java code,
- sankarmail December 26, 2013