doublepower(doublebase,intexp){if(exp==1)returnbase;doublehalf=power(base,exp>>1);return(((exp&1)==1)base:1.0)halfhalf;}
Thetrickislikethis:takeuseoftheoldpSibling,makeitpointstothenewcreatedclonednode,whilemakethenewclonednode’spNextbackuptheoldpSibling.
ComplexNodeClone(ComplexNodepHead){if(pHead==NULL)returnNULL;preClone(pHead);inClone(pHead);returnpostClone(pHead);}
voidpreClone(ComplexNodepHead){ComplexNodep=newComplexNode();p->m_pNext=pHead->m_pSibling;pHead->m_pSibling=p;if(pHead->m_pNext!=NULL)preClone(pHead->m_pNext);}
voidinClone(ComplexNodepHead){ComplexNodepSib=pNew->m_pNext;if(pSib==NULL){pNew->m_pSibling=NULL;}else{pNew->m_pSibling=pSib->m_pSibling;}if(pHead->m_pNext!=NULL)inClone(pHead->m_pNext);}
ComplexNodepostClone(ComplexNodepHead){ComplexNodepNew=pHead->m_pSibling;ComplexNodepSib=pNew->m_pNext;if(pHead->m_pNext!=NULL){pNew->m_pNext=pHead->m_pNext->m_pSibling;pHead->m_pSibling=pSib;postClone(pHead->m_pNext);}else{pNew->pNext=NULL;pHead->m_pSibling=NULL;}returnpNew;}
NodemergeSort(Nodep,intlen){if(len==1){p->next=NULL;returnp;}Nodepmid=p;for(inti=0;i
Similarly,assumeC(M,2)>=nandC(M-1,2) Alsorecursivewayworks.PossibleoptimizationslikeSundayalgorithmorRabin-Karpalgorithmwilldo. Node*array2Tree(int[]array){returnhelper(array,0,n-1);} Nodehelper(int[]array,intstart,intend){if(start>end)returnNULL;intm=start+(end-start)/2;Noderoot=newNode(array[m]);root->left=helper(array,start,m-1);root->right=helper(array,m+1,end);returnroot;} intpartitionStar(chara[]){intcount=0;inti=a.length-1,j=a.length-1;//iforthecursor,jforthefirstnon-charwhile(i>=0){if(a[i]!=‘’){swap(a,i--,j--);}else{i--;count++;}}returncount;} Asub-quadraticsolutioncanbedonebyDP. f(n)=f(n-1)+Index(n)Index(n),whichistodeterminehowmanynumbersissmallerthana[n]ina[0..n-1],canbedoneinlog(n)timeusingBSTwithsubtreesize.