// Function to convert a DLL to a Binary Tree CNode *convDLLtoBT(CNode *pHeadC) { CNode *mid; // Middle point of DLL (and will be root) CNode *subEnd; // Acts as end of left DLL CNode *rHead; // Acts as pHead of right side DLL // Base Case if(pHeadC == NULL) return NULL; // Find middle of DLL/subDLL (root) mid = findMiddle(pHeadC); // Break out left DLL if(pHeadC == mid) pHeadC = NULL; else { subEnd = mid->pPrevious; if(subEnd != NULL) subEnd->pNext = NULL; } rHead = mid->pNext; // Break out right DLL mid->pPrevious = convDLLtoBT(pHeadC); // Recursive call for left DLL mid->pNext = convDLLtoBT(rHead); // Recursive call for right DLL return mid; // Return original mid as root of binary tree }
int main() { int a[] = {2,3,2,2,2,2,1,5,4}; int result = findMiddle(a,9); printf("result is %d\n",result); getchar(); getchar(); return 0; }
ListNode* sortList(ListNode* head) { if(!head || !head->next) return head; auto middle=findMiddle(head); auto next=middle->next; middle->next=NULL; return merge(sortList(head), sortList(next)); }
int main(int argc, char **argv){ char array[] = "asdfasdfasdfasdf"; int *p = preProcess(array, 16); double *pd = preProcess2(array, 16); printf("find average 2 to 9: %d\n", findAverage(array, 16, 2, 9, p)); printf("find average 2 to 9: %d\n", findAverage2(array, 16, 2, 9, pd)); printf("find middle 2 to 9: %d\n", findMiddle(array, 16, 2, 9)); return 1; }
void reorderList(struct ListNode* head) { struct ListNode *p,*q; if(!head||!head->next||!head->next->next) return; p=findMiddle(head); q=reverse(p->next); p->next=NULL; merge(head,q); }
bool isPalindrome(ListNode* head) { ListNode* pMid = findMiddle(head); ListNode* pRev = reverseLink(pMid); for(;head!=pMid; head=head->next, pRev=pRev->next) { if (head->val != pRev->val) { return false; } } return true; }
int main() { int i; int a[] = {1,2,3,2,2,2,5,4,2}; int result = findMiddle(a, 0, 8, 9); printf("middle is %d\n", result); getchar(); getchar(); return 0; }
ListNode *sortList(ListNode *head) { if (head == NULL || head->next == NULL)return head; ListNode *middle = findMiddle(head); ListNode *head2 = middle->next; middle->next = nullptr; // 断开 ListNode *l1 = sortList(head); // 前半段排序 ListNode *l2 = sortList(head2); // 后半段排序 return mergeTwoLists(l1, l2); }
ListNode* sortList(ListNode* head) { if (!head || !head->next) { return head; } ListNode* mid = findMiddle(head); ListNode* right = sortList(mid->next); mid->next = nullptr; ListNode* left = sortList(head); return merge(left, right); }
void reorderList(ListNode *head) { if (head == nullptr) { return; } ListNode *mid = findMiddle(head); ListNode *tail = reverse(mid->next); mid->next = nullptr; merge(head, tail); return; }
int findMiddle(int* a, int start, int end, int n) { int index; int middle = -1; index = partition(a, start, end); printf("index is %d\n",index); if(index==n/2){ middle = a[index]; } else if(index>n/2){ end = index-1; return findMiddle(a, start, end, n); } else{ start = index+1; return findMiddle(a, start, end, n); } return middle; }
/** * @param head: The first node of linked list. * @return: You should return the head of the sorted linked list, using constant space complexity. */ ListNode *sortList(ListNode *head) { // write your code here if (NULL == head || NULL == head->next) { return head; } ListNode *midNode = findMiddle(head); ListNode *rList = sortList(midNode->next); midNode->next = NULL; ListNode *lList = sortList(head); return mergeList(lList, rList); }
static ListNode *sortList(ListNode *head) { if (head == nullptr || head->next == nullptr) { return head; } ListNode* midPtr = findMiddle(head); ListNode* right = sortList(midPtr->next); midPtr->next = nullptr; ListNode* left = sortList(head); return Merge(left, right); }
bool isPalindrome(ListNode* head) { //TC:O(n); SC:O(1) if(!head){ return true; } ListNode* mid = findMiddle(head); ListNode* tmp = mid->next; mid->next = nullptr; //disconnect ListNode* newHead = reverseList(tmp); while(newHead){ if(newHead->val != head->val){ return false; } newHead = newHead->next; head = head->next; } return true; }
void reorderList(ListNode *head) { if (!head) { return; } ListNode *l1 = head, *cur = head; ListNode *l2 = reverse(findMiddle(head)); bool reversed = false; while(l1 || l2) { if (!reversed) { l1 = l1 ? l1->next : l1; cur->next = l2; } else { l2 = l2 ? l2->next : l2; cur->next = l1; } cur = cur->next; reversed = !reversed; } }
int main(void) { struct node * root = NULL; insert(&root,1); insert(&root,2); insert(&root,3); insert(&root,4); insert(&root,5); insert(&root,6); insert(&root,7); struct node * temp = root; while(temp!=NULL){ printf("%d\n",temp->item); temp=temp->next; } findMiddle(root); return 0; }
void reorderList(ListNode* head) { if(!head||!head->next||!head->next->next) return; ListNode* mid = findMiddle(head); mid = rotateList(mid); head = mergeList(head,mid); }
void Touch::setContours(const vector<Point>& contours) { contourPoints = vector<Point>(contours); middle = findMiddle(); area = countArea(); }