Exemple #1
0
ListNode *Solution::sortList(ListNode *head){
	if (head==NULL || head->next==NULL)
		return head;
	int n = 0;
	for (ListNode* itr = head; itr!=NULL; itr = itr->next) n++;
	for (int width=1; width<n; width+=width){
		ListNode * lhead = head, *last=NULL;
		while(lhead != NULL){
			ListNode * ltail = lhead;
			for (int i=1; i<width && ltail->next!=NULL; i++) ltail = ltail->next;
			if (ltail->next==NULL){
			    last->next = lhead;
				break;
			}
			ListNode * rhead = ltail->next;
			ltail->next = NULL;
			ListNode * rtail = rhead;
			for (int i=1; i<width && rtail->next!=NULL; i++) rtail = rtail->next;
			ListNode * next = rtail->next;
			rtail->next = NULL;
			ListNode* merged_tail = NULL;
			ListNode* merged_head = mergeList(lhead, rhead, merged_tail);
			if (last==NULL)
				head = merged_head;
			else last->next = merged_head;
			last = merged_tail;
			lhead = next;
		}
	}
	return head;
}
Exemple #2
0
 ListNode *sortList(ListNode *head) {
     if(head==NULL||head->next==NULL) return head;
     
     ListNode *middleNode = getMiddleList(head);
     ListNode *nextNode = middleNode->next;
     middleNode->next = NULL;
     return mergeList(sortList(head), sortList(nextNode));
 }
Exemple #3
0
 void reorderList(ListNode* head)
 {
     if (head && head->next)
     {
         ListNode* pr = partationList(head);
         ListNode* pl = head;
         pr = reverseList(pr);
         head = mergeList(pl, pr);
     }
 }
Exemple #4
0
int main(int argc, const char * argv[]) {
    int a[10] = {1,3,9,8,4,2,5,0,7.6};
    int b[4] = {2,2,3};
    int c[4] = {1,3,3,4};
    SeqList list;//这个怎么直接全部赋值

    fillList(&list, a, 10);
    
    printf("获取位置:%d\n",Locate(list, 2));//按内容查找
    
    printf("插入:%s\n",InsList(&list,2,10)==1?"OK":"ERROR");//插入
    
    int delement;
    printf("删除:%s,删除的元素:%d\n",DelList(&list,3,&delement)==1?"ok":"ERROR",delement);//删除
    
    
    SeqList list1,list2,list3;
    
    fillList(&list1,b,3);
    fillList(&list2,c,4);
    
    mergeList(&list1,&list2,&list3);//合并两个非递减有序线性表
    printf("合并后的list:%d\n",list3.elem[7]);
    
    printf("---------%s---------\n","链表");
    
    //----------链表-------
    
    LinkList L;
    InitList(&L);
    
    //CreateFromHead(L);
    CreateFormTail(&L);
    
    Node *p = Get(L, 2);
    printf("链表的第2个节点:%c\n",p->data);
    
    
    Node *pp = Located(L,'2');
    printf("key是2的节点:%c\n",pp->data);
    
    printf("链表的长度是:%d\n",ListLength(L));
    
    InseList(L,1,'5');
    printf("插入后的链表长度:%d,首节点:%c\n",ListLength(L),Get(L, 1)->data);
    
    char tem;
    DellList(L,1,&tem);
    printf("链表长度%d,删除链表元素:%c\n",ListLength(L),tem);
    
    LinkList Lb = L;
    printf("合并后的长度%d\n",ListLength(MergeLinkList(L,Lb)));
    return 0;
}
Exemple #5
0
list mergeList(list a, list b)
{
    list head = NULL;

    if (a == NULL)
        return b;
    if (b == NULL)
        return a;
    if (a->data > b->data)
    {
        head = a;
        head->next = mergeList(a->next, b);
    }
    else
    {
        head = b;
        head->next = mergeList(a, b->next);
    }
    return head;
}
Exemple #6
0
ListaContato *mergeSortList(ListaContato *head, int begin, int end, ListaContato *aux){
	
	if(begin < end){

		int mid = (begin + end) / 2;
		
		aux = mergeSortList(head,begin,mid,aux);
		aux = mergeSortList(head,mid + 1,end,aux);
		aux = mergeList(head,begin,mid,end,aux);
	}
	return aux;
}
Exemple #7
0
    ListNode *sortList(ListNode *head) {

        ListNode *head2;
        if (!head || !head->next)
        {// if the list is empty or only has one node, just return
            return head;
        }
        else
        {// split the list by half, sort each of them and merge the two half lists
            head2 = splitList(head);
            return mergeList(sortList(head), sortList(head2));
        }
    }
Exemple #8
0
ListNode* sortList(ListNode* head) {
	if(!head||!head->next)  return head;
	//find the middle;
	ListNode *slow, *fast;
	for(slow = fast = head; fast->next && fast->next->next; slow = slow->next, fast = fast->next->next) ;
	fast = slow->next;
	slow->next = NULL;
	//recur sort the list
	slow = sortList(head);
	fast = sortList(fast);
	//merge two sorted list
	return mergeList(slow,fast);
}
Exemple #9
0
 void reorderList(ListNode* head) {
   if ( NULL == head || NULL == head->next ) return;
   ListNode *prev = NULL;
   ListNode *slow = head;
   ListNode *fast = head;
   while ( NULL != fast && NULL != fast->next ) {
     prev = slow;
     slow = slow->next;
     fast = fast->next->next;
   }
   prev->next = NULL;
   head = mergeList(head, reverseList(slow));
 }
ListNode* sortLinkList(ListNode *&head, int N) {
    if (N == 0) return NULL;
    if (N == 1) {
        ListNode* cur = head;
        head = head->next;
        cur->next = NULL;
        return cur;
    }
    int half = N / 2;
    ListNode* head1 = sortLinkList(head, half);
    ListNode* head2 = sortLinkList(head, N - half);
    return mergeList(head1, head2);
}
Exemple #11
0
    /**
     * @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);
    }
Exemple #12
0
    ListNode* sortList(ListNode* head) {
        if (!head) return NULL;
        if (!head->next) return head;

        ListNode* a = head, *b = head->next->next;

        while (a && b) {
            a = a->next;
            b = b->next;
            if (b) b = b->next;
        }
        ListNode* mid = a->next;
        a->next = NULL;

        return mergeList(sortList(head), sortList(mid));
    }
Exemple #13
0
ListNode *sortList(ListNode *head) {
    if( !head || !head->next ) return head;
    ListNode *head1 = head;
    ListNode *head2 = head;

    while(head2->next && head2->next->next){
        head1 = head1->next;
        head2 = head2->next->next;
    }
    head2 = head1->next;
    head1->next = NULL; // split two list
    head1 = head;
    ListNode *left = sortList(head1);
    ListNode *right = sortList(head2);
    return mergeList(left,right);
}
Exemple #14
0
    ListNode* sortList(ListNode* head)
    {
        if ( head == NULL || head->next == NULL)
        {
            return head;
        }

        ListNode* middle = findmiddle(head);

        ListNode* right = sortList(middle->next);
        middle->next = NULL;
        ListNode* left = sortList(head);

        ListNode* ret = mergeList(left, right);

        return ret;
    }
Exemple #15
0
static QStringList get_types(QStringList (*get_func)(Provider *p), const QString &provider)
{
	QStringList out;
	if(!provider.isEmpty())
	{
		Provider *p = providerForName(provider);
		if(p)
			out = get_func(p);
	}
	else
	{
		ProviderList pl = allProviders();
		foreach(Provider *p, pl)
			mergeList(&out, get_func(p));
	}
	return out;
}
Exemple #16
0
void mergeSort(struct node** head)
{
	struct node* tmp = *head;
	struct node* a = null;
	struct node* b = null;
	
	if(tmp == null || tmp->next == null)
		return;
	
	splitList(tmp, &a,&b);
	
	mergeSort(&a);
	mergeSort(&b);
	
	*head = mergeList(a, b);
		
}
Exemple #17
0
        ListNode *sortList(ListNode *head) {
            if(!head || !head->next) return head;

            ListNode *slow = head;
            ListNode *fast = head;

            while(fast->next && fast->next->next) {
                slow = slow->next;
                fast = fast->next->next;
            }

            // split linked list
            fast = slow;
            slow = slow->next;
            fast->next = nullptr;

            return mergeList(sortList(head), sortList(slow)); // merge sorted list
        }
 ListNode *sortList(ListNode *head){
     if (head == NULL)
         return head;
     else if (head->next == NULL) 
         return head;
     
     ListNode *slow = head, *fast = head;
     while (fast->next != NULL && fast->next->next != NULL)
     {
         slow = slow->next;
         fast = fast->next->next;
     }
     fast = slow;
     slow = slow->next;
     fast->next = NULL;
     
     fast = sortList(head);
     slow = sortList(slow);
     
     return mergeList(fast,slow);
     
 }
Exemple #19
0
 ListNode* sortList(ListNode* head)
 {
     int n = getLength(head);
     if (n == 0 || n == 1)
     {
         return head;
     }
     n /= 2;
     ListNode* pl = head;
     ListNode* pr = head;
     while (n)
     {
         --n;
         pl = pr;
         pr = pr->next;
     }
     pl->next = NULL;
     pl = sortList(head);
     pr = sortList(pr);
     head = mergeList(pl, pr);
     return head;
 }
 ListNode *mergeKLists(vector<ListNode *> &lists) {
     ListNode* result = NULL;
     for (int i = 0; i < lists.size(); i++)
         result = mergeList(result, lists[i]);
     return result;
 }
Exemple #21
0
int main(int argc, char *argv[]) {

        snode_t *la, *lb, *lc;

        la = (snode_t *)malloc(sizeof(snode_t));

        snode_t *node, *temp;
        node = (snode_t *)malloc(sizeof(snode_t));
        node->data = (void *)15;
        node->next = NULL;

        temp = (snode_t *)malloc(sizeof(snode_t));
        temp->data = (void *)20;
        temp->next = NULL;

        node->next = temp;

        temp = (snode_t *)malloc(sizeof(snode_t));
        temp->data = (void *)22;
        temp->next = NULL;

        node->next->next = temp;

        la->next = node;


        lb = (snode_t *)malloc(sizeof(snode_t));

        snode_t *node2, *temp2;
        node2 = (snode_t *)malloc(sizeof(snode_t));
        node2->data = (void *)11;
        node2->next = NULL;

        temp2 = (snode_t *)malloc(sizeof(snode_t));
        temp2->data = (void *)21;
        temp2->next = NULL;

        node2->next = temp2;

        temp2 = (snode_t *)malloc(sizeof(snode_t));
        temp2->data = (void *)33;
        temp2->next = NULL;

        node2->next->next = temp2;

        lb->next = node2;

        snode_t *current;
        current = la->next;

        while (current) {
                printf("%d\n", (unsigned int)current->data);
                current = current->next;
        }
        
        current = lb->next;

        while (current) {
                printf("%d\n", (unsigned int)current->data);
                current = current->next;
        }


        lc = (snode_t *)malloc(sizeof(snode_t));
        lc->data = (void *)0;
        lc->next = NULL;

        mergeList(la, lb, &lc);

	
        printf("after merge\n");
        current = lc->next;

        while (current) {
                printf("%d\n", (unsigned int)current->data);
                current = current->next;
        }
        return 0;
}
 ListNode* sortList(ListNode* head) {
     if (!head || !head->next) return head;
     ListNode* mid = getMid(head);
     return mergeList(sortList(head), sortList(mid));
 }
 void reorderList(ListNode* head) {
     if(!head||!head->next||!head->next->next) return;
     ListNode* mid = findMiddle(head);
     mid = rotateList(mid);
     head = mergeList(head,mid);
 }