void reorderList(ListNode *head) {
     //O(n), O(1),第一个节点位置不变
     if(head == NULL)
         return;
     //找链表中点
     ListNode *mid = findMid(head);
     //将链表切成两段
     ListNode *firstHalf = head;
     ListNode *lastHalf = mid->next;
     mid->next = NULL;
     //将链表后半部反向成为reverseList
     lastHalf = reverseList(lastHalf);
     //循环,间隔插入
     ListNode *tail = firstHalf;
     firstHalf = firstHalf->next;
     while(firstHalf != NULL && lastHalf != NULL){
         tail->next = lastHalf;
         tail = tail->next;
         lastHalf = lastHalf->next;
         
         tail->next = firstHalf;
         tail = tail->next;
         firstHalf = firstHalf->next;
     }
     if(firstHalf != NULL)
         tail->next = firstHalf;
     if(lastHalf != NULL)
         tail->next = lastHalf;
 }
예제 #2
0
 void reorderList(ListNode* head) {
     if (head == NULL || head -> next == NULL) {
         return;
     }
     ListNode * mid = findMid(head);
     ListNode * tail = reverse(mid -> next);
     mid -> next = NULL;
     merge(head, tail);
 }
struct TreeNode* buildTree(int* inorder, int inorderSize, int* postorder, int postorderSize) {
    if(inorderSize<=0||postorderSize<=0)
        return NULL;
    struct TreeNode *root=malloc(sizeof(struct TreeNode));
    int mid=postorder[postorderSize-1];
    int left=findMid(inorder,inorderSize,mid);
    root->val=mid;
    root->left=buildTree(inorder,left,postorder,left);
    root->right=buildTree(inorder+left+1,inorderSize-left-1,postorder+left,postorderSize-left-1);
    return root;
}
예제 #4
0
 ListNode *sortList(ListNode *head) {
     if(head == NULL || head->next == NULL) {
         return head;
     }
     
     ListNode* mid = findMid(head);
     ListNode* right = mid->next;
     ListNode* left = head;
     mid->next = NULL;
     
     left = sortList(left);
     right = sortList(right);
     
     return mergeTwoLists(left, right);
     
 }
예제 #5
0
 TreeNode* sortedListToBST(ListNode* head) {
     if(head == NULL)
         return NULL;
     pair<ListNode *, ListNode *> mid = findMid(head);
     if(mid.second == NULL)
         return NULL;
     cout << mid.second->val << endl;
     TreeNode *root = new TreeNode(mid.second->val);
     if(mid.first) {
         mid.first->next = NULL;
         root->left = sortedListToBST(head);
     }
     if(mid.second->next) {
         root->right = sortedListToBST(mid.second->next);
     }
     return root;
 }
예제 #6
0
//3. 用递归来sort整个list
Node listSort(Node head) {
    Node sorted, l1, l2, item, tem;
    if(head->next == NULL) {
        return head;
    }
    else {
        //分半
        l1 = head;
        tem = findMid(head);
        l2 = tem->next;
        tem->next = NULL;
        //sort每半
        l1 = listSort(l1);
        l2 = listSort(l2);
        //merge两半
        sorted = mergeSortedList(l1,l2);

        return sorted;
    }
}
예제 #7
0
파일: helpers.c 프로젝트: soitknows/cs50
bool binary(int key, int *array, int min, int max)
{
    if (max < min)
    {
        return false;
    }
    else
    {
        int mid = findMid(min, max);
        if (array[mid] < key)
        {
            return binary(key, array, mid + 1, max);
        }
        else if(array[mid] > key)
        {
            return binary(key, array, min, mid - 1);
        }
        else
        {
            return true;
        }
    }
}
예제 #8
0
int bSearch(int *ARRAY, int SIZE, int Key)
{
	int min = 0;
	int max = SIZE - 1;
	int Found = -1;
	
	
	if(Inside(ARRAY[min],ARRAY[max],Key))
	{
		int mid = 0;
		while(min < max)
		{
			mid = findMid(min,max);
			if((Found = search(ARRAY,&min,mid,&max,Key) != -1))
			{
				break;
			}
		}	return Found;
	}
	else
	{
		return Found;
	}	
}	
예제 #9
0
int main()
{
	struct list *l1 , *l2 , *l3 ;
	l1 = malloc(sizeof(struct list)) ;
	l2 = malloc(sizeof(struct list)) ;
	l3 = malloc(sizeof(struct list)) ;

	l1 -> data = 1 ;
	l2 -> data = 10 ;
	l3 -> data = 100 ;

	l1 -> next = NULL ;
	l2 -> next = NULL ;
	l3 -> next = NULL ;

	int i ;
	for(i = 2 ; i < 7 ; i++)
	{
		struct list *newNode = malloc(sizeof(struct list)) ;
		newNode -> data = i ;
		newNode -> next = l1 ;
		l1 = newNode ;
	}


	for(i = 2 ; i < 5 ; i++)
	{
		struct list *newNode = malloc(sizeof(struct list)) ;
		newNode -> data = i*10 ;
		newNode -> next = l2 ;
		l2 = newNode ;
	}


		for(i = 2 ; i < 4 ; i++)
	{
		struct list *newNode = malloc(sizeof(struct list)) ;
		newNode -> data = i*100 ;
		newNode -> next = l3 ;
		l3 = newNode ;
	}


	struct list *ptr , *ptr2 , *ptr3 ;
	ptr = l1 ;
	ptr2 = l2 ;
	ptr3 = l3 ;

	while(ptr -> next != NULL)
	{
		ptr = ptr -> next ;
	}

	while(ptr2 -> next != NULL)
	{
		ptr2 = ptr2 -> next ;
	}

	ptr -> next = ptr3 ;
	ptr2 -> next = ptr3 ;

	display(l1) ;

	findMid(l1) ; // this function find the mid element of the list with a single scan 

	printf("\n");

	display(l2) ;


	// here we have two lists that combined with third list

	ptr = l1 ;
	ptr2 = l2 ;

	int flag = 0 ;

	while(ptr -> next != NULL)
	{
		ptr2 = l2 ;
		while(ptr2 -> next != NULL)    // we find the intersecting node in complexity of O(nm) ;
		{
			if(ptr2 == ptr)
			{
				flag = 1 ;
				break ;
			}
			ptr2 = ptr2 -> next ;
		}
		if(flag == 1)
			break ;

		ptr = ptr -> next ;
	}

	printf("Both linked list joined at : %d\n", ptr -> data );

	// another approach to find the interecting node with complexity of max(O(n) , O(m))

	getIntersect(l1 , l2) ;

}