ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
     // Note: The Solution object is instantiated only once and is reused by each test case.
     
     if(l1 == NULL)
     {
         return l2;
     }
     else if(l2 == NULL)
     {
         return l1;
     }
     
     ListNode * mergeHead;
     if(l1->val > l2->val)
     {
         mergeHead = l2;
         mergeHead -> next = mergeTwoLists(l1,l2->next);
     }
     else
     {
         mergeHead = l1;
         mergeHead -> next = mergeTwoLists(l1->next,l2);
     }
     return mergeHead;
 }
Ejemplo n.º 2
0
 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
     if(l1==NULL)
     {
         return l2;
     }
     if(l2==NULL)
     {
         return l1;
     }
     ListNode* lNode1=l1;
     ListNode* lNode2=l2;
     ListNode* newhead;
     if(lNode1!=NULL&&lNode2!=NULL)
     {
         if(lNode1->val>lNode2->val)//l2小,所以头结点
         {
             newhead=lNode2;
             lNode2->next=mergeTwoLists(lNode1,lNode2->next);
         }
         else//l1小,所以头结点
         {
             newhead=lNode1;
             lNode1->next=mergeTwoLists(lNode1->next,lNode2);
         }
     }
     return newhead;
     
 }
Ejemplo n.º 3
0
	ListNode* DivideAndConque(std::vector<ListNode*>& lists, int begin, int end)
	{
		if (begin >= end || lists.size() == 0)
			return NULL;

		if (begin + 1 == end)
			return lists[begin];

		ListNode* left = NULL;
		ListNode* right = NULL;

		
		int n = end - begin;
		// try to eliminate the level of recursion
		if (n == 2)
		{
			left = lists[begin];
			right = lists[begin+1];
		}
		else if (n == 3)
		{
			left = lists[begin];
			right = mergeTwoLists(lists[begin + 1], lists[begin + 2]);
		}
		else
		{
			int mid = n / 2 + begin;
			left = DivideAndConque(lists, begin, mid);
			right = DivideAndConque(lists, mid, end);
		}


		return mergeTwoLists(left, right);
	}
 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
     if (l1 == NULL && l2 == NULL) return NULL;
     if (l1 == NULL) return l2;
     if (l2 == NULL) return l1;
     if (l1->val < l2->val) {
         l1->next = mergeTwoLists(l1->next, l2);
         return l1;
     } else {
         l2->next = mergeTwoLists(l1, l2->next);
         return l2;
     }
 }
Ejemplo n.º 5
0
 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
     if(!l1) return l2;
     if(!l2) return l1;
     if(l1->val < l2->val){
         l1->next = mergeTwoLists(l1->next, l2);
         return l1;
     }
     else{
         l2->next = mergeTwoLists(l1, l2->next);
         return l2;
     }
 }
Ejemplo n.º 6
0
//Others
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    if (l1 == NULL)
        return l2;
    if (l2 == NULL)
        return l1;
    if (l1->val <= l2->val) {
        l1->next = mergeTwoLists(l1->next, l2);
        return l1;
    } else {
        l2->next = mergeTwoLists(l1, l2->next);
        return l2;
    }
}
Ejemplo n.º 7
0
struct ListNode *mergeKLists(struct ListNode **lists, int listsSize) {
    if (listsSize == 0 || lists == NULL) {
        return NULL;
    }
    if (listsSize == 1) {
        return lists[0];
    }
    if (listsSize == 2) {
        return mergeTwoLists(lists[0], lists[1]);
    }
    struct ListNode *a = mergeKLists(lists, listsSize / 2);
    struct ListNode *b = mergeKLists(lists + listsSize / 2, listsSize - listsSize / 2);
    return mergeTwoLists(a, b);
}
Ejemplo n.º 8
0
 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
     if (l1 == nullptr) return l2;
     if (l2 == nullptr) return l1;
     
     if (l1->val <= l2->val)
     {
         l1->next = mergeTwoLists(l1->next, l2);
         return l1;
     }
     else
     {
         l2->next = mergeTwoLists(l1, l2->next);
         return l2;
     }
 }
Ejemplo n.º 9
0
int main(int argc, char *argv[])
{
    struct ListNode *ln1 = malloc(sizeof(struct ListNode));
    struct ListNode *ln2 = malloc(sizeof(struct ListNode));
    struct ListNode *ln3 = malloc(sizeof(struct ListNode));
    struct ListNode *ln4 = malloc(sizeof(struct ListNode));

    ln1->val = 2;
    ln1->next = ln2;
    ln2->val = 3;
    ln2->next = ln3;
    ln3->val = 6;
    ln3->next = ln4;
    ln4->val = 8;
    ln4->next = NULL;

    struct ListNode *ln5 = malloc(sizeof(struct ListNode));
    struct ListNode *ln6 = malloc(sizeof(struct ListNode));
    struct ListNode *ln7 = malloc(sizeof(struct ListNode));
    struct ListNode *ln8 = malloc(sizeof(struct ListNode));

    ln5->val = 4;
    ln5->next = ln6;
    ln6->val = 5;
    ln6->next = ln7;
    ln7->val = 7;
    ln7->next = NULL;

    struct ListNode *result = mergeTwoLists(ln1, ln5);

}
Ejemplo n.º 10
0
 ListNode* mergeKLists(vector<ListNode*>& lists, int low, int high) {
     if(high > low) {
         return mergeTwoLists(mergeKLists(lists, low, (low+high)/2), mergeKLists(lists, (low+high)/2+1, high));
     } else {
         return lists[low];
     }
 }
Ejemplo n.º 11
0
TEST(MergeTwoSortedListsTestCase, ListIsNull)
{
    ListNode* List_A = new ListNode(2);
    ListNode* List_B = new ListNode(1);

    AddListNode(List_A, new ListNode(4));
    AddListNode(List_A, new ListNode(6));
    AddListNode(List_A, new ListNode(10));

    PrintList(List_A);
    PrintList(List_B);

    EXPECT_EQ(List_A, mergeTwoLists(List_A, NULL));
    EXPECT_EQ(List_B, mergeTwoLists(NULL, List_B));
    EXPECT_EQ(NULL, mergeTwoLists(NULL, NULL));
}
Ejemplo n.º 12
0
 ListNode *mergeKLists(vector<ListNode *> &lists,unsigned int s,unsigned int e) {
     if(s == e)
     {
         return lists[s];
     }
     if(s + 1 == e)
     {
         return mergeTwoLists(lists[s],lists[e]);
     }
     else
     {
         ListNode* left = mergeKLists(lists,s,s + (e - s)/2);
         ListNode* right = mergeKLists(lists,s + (e - s)/2 + 1,e);
         return mergeTwoLists(left,right);
     }
 }
Ejemplo n.º 13
0
/* recursive version */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    if(l1 == NULL)
        return l2;
    if(l2 == NULL)
        return l1;
    
    struct ListNode *head;
    if(l1 -> val <= l2 -> val) {
        head = l1;
        head -> next = mergeTwoLists(l1 -> next, l2);
    } else {
        head = l2;
        head -> next = mergeTwoLists(l1, l2 -> next);
    }
    return head;
}
    ListNode * rec(vector<ListNode*>& lists, int start, int end) {
        /* Base cases */
        if(start==(end-1)) 
           return  mergeTwoLists(lists[start],lists[end]);
        else if(start==end) /*Odd case */ 
            return lists[start];
         else if(start>end)
            return NULL;

        int mid = (start+end)/2;
        auto first = rec(lists, start, mid);
        auto sec = rec(lists, mid+1,end);
 
        return mergeTwoLists(first, sec);
        
    }
Ejemplo n.º 15
0
 ListNode *mergeKLists(vector<ListNode *> &lists, int begin, int end) {
     if(begin > end)
         return NULL;
     if(begin == end)
         return lists[begin];
     return mergeTwoLists(mergeKLists(lists, begin, (begin + end) / 2), mergeKLists(lists, (begin + end) / 2 + 1, end));
 }
Ejemplo n.º 16
0
 ListNode* merge(vector<ListNode*>& lists, int l, int r) {
   if ( l < r ) {
     int m = (l+r)/2;
     return mergeTwoLists(merge(lists, l, m), merge(lists, m+1, r));
   }
   return lists[l];
 }
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    if(l1==NULL)
        return l2;
    if(l2==NULL)
        return l1;

    struct ListNode* result=NULL;
    if(l1->val<l2->val){
        result=l1;
        result->next=mergeTwoLists(l1->next,l2);
    }
    else{
        result=l2;
        result->next=mergeTwoLists(l1,l2->next);
    }
    return result;
}
 ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
     ListNode *head;
     if(l1==NULL&&l2!=NULL) return l2;
     else if(l1==NULL&&l2==NULL) return NULL;
     else if(l1!=NULL&&l2==NULL) return l1;
     else {
         if(l1->val<l2->val) {
             head=l1;
             head->next=mergeTwoLists(l1->next,l2);
         }
         else {
             head=l2;
             head->next=mergeTwoLists(l1,l2->next);
         }
         return head;
     }
 }
Ejemplo n.º 19
0
/*
 * Recursive solution to merge two sorted lists. First, check the
 * base conditions. After that, if value in node of one list is 
 * lesser than/equal to value in node of other list, update the
 * the result list 'n' as that. n's next will be a recursive
 * computation of the updated list and its counterpart.
 */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    
    struct ListNode* n = NULL;
    
    if (!l1 && !l2) return NULL;
    if (l1 && !l2) return l1;
    if (l2 && !l1) return l2;
    
    if (l1->val <= l2->val) {
        n = l1;
        n->next = mergeTwoLists(l1->next, l2);
    } else {
        n = l2;
        n->next = mergeTwoLists(l1, l2->next);
    }
    return n;
}
Ejemplo n.º 20
0
 ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
     if(l1 == NULL)
         return l2;
     else if(l2 == NULL)
         return l1;
     ListNode* p;
     if(l1->val < l2->val)
     {
         p = l1;
         p->next = mergeTwoLists(l1->next, l2);
     }
     else
     {
         p = l2;
         p->next = mergeTwoLists(l1, l2->next);
     }
     return p;
 }
    ListNode *mergeKLists(vector<ListNode *> &lists) {
        if (lists.size() == 0) return nullptr;

        ListNode *p = lists[0];
        for (int i = 1; i < lists.size(); i++) {
            p = mergeTwoLists(p, lists[i]);
        }
        return p;
    }
Ejemplo n.º 22
0
struct ListNode* merge(struct ListNode** lists,int beg,int end) {
	if(beg>end)return NULL;
	else if(beg==end)return lists[beg];
	else {
		int mid=beg+end>>1;
		struct ListNode* left=merge(lists,beg,mid),
					*right=merge(lists,mid+1,end);
		return mergeTwoLists(left,right);
	}
}
Ejemplo n.º 23
0
struct ListNode* mergeKLists(struct ListNode** lists, int listsSize) {
    int len = listsSize; 
    while (len > 1) {
        for (int i = 0; i < len / 2; ++i) {
            lists[i] = mergeTwoLists(lists[i], lists[len - 1 - i]);
        }
        len = (len + 1) / 2;
    }
    return lists[0];
}
Ejemplo n.º 24
0
 /**
  * @param ListNode l1 is the head of the linked list
  * @param ListNode l2 is the head of the linked list
  * @return: ListNode head of linked list
  */
 ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
     // write your code here
     if(l1 == NULL) {
         return l2;
     }
     if(l2 == NULL) {
         return l1;
     }
     ListNode *head;
     if(l1->val <= l2->val) {
         head = l1;
         head->next = mergeTwoLists(l1->next, l2);
     }
     else {
         head = l2;
         head->next = mergeTwoLists(l1, l2->next);
     }
     return head;
 }
Ejemplo n.º 25
0
 ListNode *mergeKLists(vector<ListNode *> &lists) {
     if(lists.size() == 0) {
         return NULL;
     }
     ListNode *res = lists[0];
     for(int i = 1; i < lists.size(); i++) {
         res = mergeTwoLists(res, lists[i]);
     }
     return res;
 }
Ejemplo n.º 26
0
ListNode* mergeKLists(std::vector<ListNode*>& lists)
{
    if (lists.empty()) return nullptr;
    if (lists.size() == 1) return lists.front();

    std::function<ListNode*(ListNode*, ListNode*)> mergeTwoLists = [&](ListNode* l1, ListNode* l2) -> ListNode*
    {
        if (l1 == nullptr && l2 == nullptr) return nullptr;
        else if (l1 == nullptr) return l2;
        else if (l2 == nullptr) return l1;
        else if (l1->val > l2->val)
        {
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }
        else
        {
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        }
    };

    for (;;)
    {
        if (lists.size() == 1)
            break;

        std::vector<ListNode*> dummyLists;
        int i = 0;
        for (int iEnd = lists.size()-1; i < iEnd; i += 2)
        {
            auto list = mergeTwoLists(lists[i], lists[i+1]);
            dummyLists.emplace_back(list);
        }

        if (i < (int)lists.size())
            dummyLists.emplace_back(lists[i]);

        lists = dummyLists;
    }

    return lists.front();
}
Ejemplo n.º 27
0
 ListNode *mergeKLists(vector<ListNode *> &lists) {
     if( lists.size() < 1 ) return NULL;
     
     ListNode* head = lists[0];
     
     for( int i = 1; i < lists.size(); ++i ) {
         head = mergeTwoLists( head, lists[i] );
     }
     
     return head;
 }
Ejemplo n.º 28
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* mergeKLists(vector<ListNode*>& lists) {
      int range = lists.size();
      int interval = 1;
      while(interval < range){
	for(size_t i = 0; i < (range - interval); i += interval*2){
	  lists[i] = mergeTwoLists(lists[i], lists[i+interval]);
	}
	interval *= 2;
      }
      return (range != 0)? lists[0] : NULL;
    }
int main() {
    int a[] = {1, 5};
    int b[] = {1, 3, 7};
    ListNode *la = initList(a, sizeof(a) / sizeof(int));
    ListNode *lb = initList(b, sizeof(b) / sizeof(int));
    printfList(la);
    printfList(lb);
    ListNode *lc = mergeTwoLists(la, lb);
    printfList(lc);
    return 0;
}