Example #1
0
int main()
{
    Solution solution;
    
    //Test cases
    {
        ListNode n1(1), n2(5), n3(8), n4(2), n5(4);
        n1.next = &n2;
        n2.next = &n3;

        n4.next = &n5;
        print_list_node(solution.mergeTwoLists(&n1, &n4));
    }
    
    {
        ListNode n1(2), n2(5), n3(8), n4(2), n5(4);
        n1.next = &n2;
        n2.next = &n3;

        n4.next = &n5;
        print_list_node(solution.mergeTwoLists(&n1, &n4));
    }
	
    {
        ListNode n1(2), n2(5), n3(8);
        n1.next = &n2;
        n2.next = &n3;

        print_list_node(solution.mergeTwoLists(&n1, NULL));
    }
	
    //Error test cases from leetcode.com
	
	return 0;
}
int main(int argc, char* argv[])
{
    Solution s;
    
    ListNode n1(1);
    ListNode n3(3);
    ListNode n5(5);
    ListNode n2(2);
    ListNode n4(4);
    ListNode n6(6);
    n1.next = &n3;
    n3.next = &n5;
    n2.next = &n4;
    n4.next = &n6;

    ListNode *ret1 = s.mergeTwoLists(&n1, &n2);
    int i = 0;
    while (ret1 != NULL)
    {
        i++;
        assert(ret1->val == i);
        ret1 = ret1->next;
    }

    i = 2;
    ret1 = s.mergeTwoLists(&n1, NULL);
    while (ret1 == NULL)
    {
        assert(ret1->val == 2);
        i += 2;
        ret1 = ret1->next;
    }

    return 0;
}
int main(){
	ListNode *l1 = new ListNode(0);
	ListNode *l10=l1;
	for (int i=0;i<5;i++){
		l1->next = new ListNode(2*i);
		l1=l1->next;
	}
        ListNode *l2 = new ListNode(1);
	ListNode *l20=l2;
        for (int i=0;i<10;i++){
                l2->next = new ListNode(2*i+1);
		l2 = l2->next;
        }
	Solution sol;
	// test merge 2 lists
	ListNode *out =  sol.mergeTwoLists(l10,l20);

	cout<<"check"<<endl;
	// test merge k lists
	vector<ListNode *> l(3,l10);
	out = sol.mergeKLists(l);
	while(out!=NULL){
		cout<<out->val<<endl;
		out=out->next;
	}
	return 1;
}
int main(int argc, char* argv[]) {
    int A1[7] = {1, 2, 2, 4, 5, 7, 9};
    int n1 = 7;
    int A[5] = {2,3,6,7,8};
    int n = 5;
    cout << "A[] is: " << endl;
    copy(A, A+n, ostream_iterator<int>(cout, " "));
    cout << endl;
    Node* merge_left = make_list_from_array(A, n);
    cout << "List of this array is :" << endl;
    print_list_node(merge_left);

    cout << "A1[] is: " << endl;
    copy(A1, A1+n1, ostream_iterator<int>(cout, " "));
    cout << endl;
    Node* merge_right = make_list_from_array(A1, n1);
    cout << "List of this array is :" << endl;
    print_list_node(merge_right);

    Solution s;
    Node* head = s.mergeTwoLists(merge_left, merge_right);
    cout << "After process List is :" << endl;
    print_list_node(head);
    return 0;
}
Example #5
0
int main(){
	Solution soln;
	ListNode one(1);
//	ListNode two(2);
//	ListNode three(3);
//	ListNode four(4);
//	ListNode five(5);

//	one.next = &two;
//	two.next = &three;
//	three.next = &four;
//	four.next = &five;

	ListNode one2(2);
//	ListNode two2(3);
//	ListNode three2(5);
//	ListNode four2(7);
//	ListNode five2(9);

//	one2.next = &two2;
//	two2.next = &three2;
//	three2.next = &four2;
//	four2.next = &five2;

	ListNode* result = soln.mergeTwoLists(&one,&one2);
	while(result != NULL){
		cout << result->val <<" ";
		result = result->next;
	}
	cout << endl;
}
Example #6
0
int main()
{
	ListNode *a1 = new ListNode(1);
	ListNode *a2 = new ListNode(4);
	ListNode *c1 = new ListNode(5);
	ListNode *c2 = new ListNode(10);
	ListNode *c3 = new ListNode(11);
	ListNode *b1 = new ListNode(2);
	ListNode *b2 = new ListNode(6);
	ListNode *b3 = new ListNode(9);

	a1->next = a2;
	a2->next = c1;
	c1->next = c2;
	c2->next = c3;
	c3->next = NULL;
	b1->next = b2;
	b2->next = b3;
	b3->next = NULL;

	Solution solute;
	ListNode * head = solute.mergeTwoLists(a1,b1);

	while(head!=NULL){
		printf("%d\t",head->val);
		head = head->next;
	}
	return 0;
}
Example #7
0
int main()
{
    ListNode *l1 = NULL;
    ListNode *l2 =NULL;
    ListNode n0(-10);
    ListNode n1(-10);
    ListNode n2(-9);
    ListNode n3(-4);
    ListNode n4(1);
    ListNode n5(6);
    ListNode n6(6);
    ListNode n7(-7);
    //ListNode n3(-4);
    l1 = &n0;
    n0.next = &n1;
    n1.next = &n2;
    n2.next = &n3;
    n3.next = &n4;
    n4.next = &n5;
    n5.next = &n6;
    l2 = &n7;

    Solution s;
    ListNode *l = s.mergeTwoLists(l1,l2);
    while(l!=NULL){
        cout<<l->val<<endl;
        l = l->next;
    }
    //cout <<flush<< c<< endl;
    return 0;
}
int main() {
    ListNode node10(1);
    ListNode node11(3);
    ListNode node12(5);
    ListNode node13(7);
    ListNode node14(9);
    node10.next = &node11;
    node11.next = &node12;
    node12.next = &node13;
    node13.next = &node14;
    cout << "List 1:" << endl;
    printList(&node10);

    ListNode node20(0);
    ListNode node21(2);
    ListNode node22(4);
    ListNode node23(6);
    ListNode node24(8);
    ListNode node25(10);
    node20.next = &node21;
    node21.next = &node22;
    node22.next = &node23;
    node23.next = &node24;
    node24.next = &node25;
    cout << "List 2:" << endl;
    printList(&node20);

    Solution sol;
    ListNode *result = sol.mergeTwoLists(&node10, &node20);
    cout << "After merge: " << endl;
    printList(result);
}
int main(){
    ListNode *head1, *head2;
    ListNode *last;
    
    for(int i = 0;i<11;i+=2){
        ListNode *tempNode = new ListNode(i);
        if(i == 0){
            head1 = tempNode;
            last = head1;
        } else {
            last->next = tempNode;
            last = tempNode;
        }
    }

    for(int i = 1;i<11;i+=2){
        ListNode *tempNode = new ListNode(i);
        if(i == 1){
            head2 = tempNode;
            last = head2;
        } else {
            last->next = tempNode;
            last = tempNode;
        }
    } 

    Solution newSolution;
    ListNode *newHead = newSolution.mergeTwoLists(head1, head2);
    //ListNode *newHead = head;
    while(newHead != NULL){
        cout << newHead->val << ' ';
        newHead = newHead->next;
    }
    return 0;
}
Example #10
0
int main()
{
	Solution s;
	if(!s.mergeTwoLists(NULL,NULL))
		cout<<"空"<<endl;
	return 0;
}
Example #11
0
int main()
{
	ListNode *h1 = new ListNode(1);
	ListNode *h2 = new ListNode(5);
	ListNode *t1 = h1;
	ListNode *t2 = h2;
	for(int i = 2; i < 5; i++)
	{
		t1->next = new ListNode(i);	
		t1 = t1->next;
		cout<<t1->val<<" ";
	}
	cout<<endl;
	for(int i = 6; i < 9; i++)
    {
    	t2->next = new ListNode(i);	
    	t2 = t2->next;
		cout<<t2->val<<" ";
    }
	cout<<endl;
	Solution s;
	ListNode *h = s.mergeTwoLists(h1,h2);
	ListNode *t = h;
	while(t)
	{
		cout<<t->val<<" ";
		t = t->next;
	}
	cout<<endl;
}
Example #12
0
int main()
{
	ListNode a1(0);
	ListNode a2(2);
	ListNode a3(4);
	ListNode a4(6);
	a1.next = &a2;
	a2.next = &a3;
	a3.next = &a4;

	ListNode b1(1);
	ListNode b2(3);
	ListNode b3(5);
	ListNode b4(7);
	b1.next = &b2;
	b2.next = &b3;
	b3.next = &b4;

	Solution s;
	ListNode* result = s.mergeTwoLists(&a1, &b1);

	while (result != NULL)
	{
		cout << result->val << endl;
		result = result->next;
	}

	system("PAUSE");
	return 0;
}
int main(){
    Solution obj;

    ListNode *h1;
    ListNode an1(2); 
    ListNode an2(7);
    ListNode an3(9);
    ListNode an4(10);
    ListNode an5(11);
    h1 = &an1; an1.next = &an2; an2.next = &an3; an3.next = &an4; an4.next = &an5;

    obj.print(h1);

    ListNode *h2;
    ListNode bn1(1);
    ListNode bn3(7);
    ListNode bn2(3);
    ListNode bn4(8);
    h2 = &bn1; bn1.next = &bn2; bn2.next = &bn3; bn3.next = &bn4;

    obj.print(h2);

    ListNode * h = obj.mergeTwoLists(h1,h2);
    obj.print(h);

    cout<<"here"<<endl;
    obj.print(h1);
    obj.print(h2);
    return 0;
}
Example #14
0
int _tmain(int argc, _TCHAR* argv[])
{
	int array1[1] = { 3};
	int array2[1] = {  7 };
	ListNode* a1 = new ListNode(-9); ListNode* head1 = a1;
	ListNode* a2 = new ListNode(5); ListNode* head2 = a2;
	for (auto i : array1)
	{
	ListNode* a = new ListNode(i);
	a1->next = a;
	a1 = a;

	}

	for (auto i : array2)
	{
		ListNode* a = new ListNode(i);
		a2->next = a;
		a2 = a;
	}

	Solution so;
	so.mergeTwoLists(head1, head2);
	return 0;
}
int main()
{
        ListNode *h1 = NULL;
        ListNode *h2 = NULL;

///*
        ListNode **p = &h1;
        for (int i = 0; i < 2; i++) {
                ListNode *t = new ListNode(i+i*i);
                *p = t;
                p = &t->next;
        }
        pl(h1);

///*
        p = &h2;
        for (int i = 0; i < 1; i++) {
                ListNode *t = new ListNode(2*i+i*i+1);
                *p = t;
                p = &t->next;
        }

        pl(h2);
//*/

        Solution s;
        ListNode *h3 = s.mergeTwoLists(h1, h2);
        pl(h3);
        return 0;
}
Example #16
0
int main()
{
    ListNode * l1_1 = new ListNode(-10);
    ListNode * l1_2 = new ListNode(-10);
    ListNode * l1_3 = new ListNode(-9);
    ListNode * l1_4 = new ListNode(-4);
    ListNode * l1_5 = new ListNode(-1);
    ListNode * l1_6 = new ListNode(6);
    ListNode * l1_7 = new ListNode(6);
    ListNode * l2_1 = new ListNode(-7);

    l1_1->next=l1_2;
    l1_2->next=l1_3;
    l1_3->next=l1_4;
    l1_4->next=l1_5;
    l1_5->next=l1_6;
    l1_6->next=l1_7;

    Solution slt;
    ListNode * result = slt.mergeTwoLists(l1_1,l2_1);

    while(result!=NULL)
    {
//        cout<<"res"<<endl;
        cout<<result->val<<"\t";
        result=result->next;
    }
    cout<<endl;
    while(1);

}
int main(){
	/*ListNode * a1 = new ListNode(1);
	ListNode * a2 = new ListNode(1);
	ListNode * a3 = new ListNode(2);
	ListNode * a4 = new ListNode(5);
	
	a1->next = a2;
	a2->next = a3;
	a3->next = a4;*/
	ListNode * a1 = NULL;
	
	ListNode * b1 = new ListNode(2);
	ListNode * b2 = new ListNode(2);
	ListNode * b3 = new ListNode(3);
	ListNode * b4 = new ListNode(6);
	ListNode * b5 = new ListNode(7);
	
	b1->next = b2;
	b2->next = b3;
	b3->next = b4;
	b4->next = b5;
	
	
	Solution s;
	ListNode * head = s.mergeTwoLists(a1,b1);
	while(head != NULL){
		cout<<head->val<<" ";
		head = head->next;
	}
}
Example #18
0
int main(){
    Solution sol;

    node_ptr l6 = new ListNode(6);
    node_ptr l5 = new ListNode(4);
    node_ptr l4 = new ListNode(2);
    l5->next = l6;
    l4->next = l5;

    node_ptr l3 = new ListNode(5);
    node_ptr l2 = new ListNode(3);
    node_ptr l1 = new ListNode(1);
    l2->next = l3;
    l1->next = l2;

    printf("\nThe original lists are: \n");
    printf("List 1: ");
    auto tmp = l1;
    while(tmp){cout << tmp->val << " "; tmp = tmp->next;} cout << "\n";
    printf("List 2: ");
    auto tmp2 = l4;
    while(tmp2){cout << tmp2->val << " "; tmp2 = tmp2->next;} cout << "\n";

    printf("\nThe merged list is given by:\n");
    auto tmp3 = sol.mergeTwoLists(l1, l4);
    while(tmp3){cout << tmp3->val << " "; tmp3 = tmp3->next;} cout << "\n\n";



    return 0;
}
Example #19
0
int main(void)
{
	ListNode* l1 = new ListNode(1, new ListNode(3, new ListNode(5, new ListNode(10, NULL))));
	ListNode* l2 = new ListNode(0, new ListNode(2, new ListNode(9, NULL)));
	Solution s;
	ListNode* ret = s.mergeTwoLists(l1, l2);
	Print(ret);
	return 0;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    Solution *a = new Solution();
    ListNode* b = new ListNode(2);
    ListNode* c = new ListNode(1);
    
    ListNode* d = a->mergeTwoLists(b, c);
    return 0;
}
int main(void)
{
	ListNode *l1=NULL,*l2=NULL;
	Solution mysolution;
	int a[]={1};
	int b[]={};
	CreateList(&l1,a,(int)sizeof(a)/sizeof(*a));
	CreateList(&l2,b,(int)sizeof(b)/sizeof(*b));
	PrintList(mysolution.mergeTwoLists(l1,l2));
	return 1;
}
void main()
{
	Solution s;
	ListNode n1(1), n2(3), n3(5), n4(2), n5(4), n6(6);
	n1.next = &n2;
	n2.next = &n3;
	n4.next = &n5;
	n5.next = &n6;
	ListNode* l1 = &n1, *l2 = &n4;
	print(s.mergeTwoLists(l1, l2));
}
int main() {
    Solution *s = new Solution();
    ListNode *l1 = new ListNode(2);
    //l1 = insert(l1, 4);
    //l1 = insert(l1, 3);
    print(l1);
    ListNode *l2 = new ListNode(1);
    //l2 = insert(l2, 6);
    //l2 = insert(l2, 4);
    print(l2);
    ListNode* r = s->mergeTwoLists(l1, l2);
    print(r);
    return 0;
}
Example #24
0
		void assert_helper(vector<int> list1, vector<int> list2, vector<int> expected){

			auto l1 = ConstructList(list1);
			auto l2 = ConstructList(list2);

			Solution sln;
			auto actual = sln.mergeTwoLists(l1, l2);

			for (auto v : expected){
				Assert::AreEqual(v, actual->val);
				actual = actual->next;
			}
			Assert::IsNull(actual);
		}
int main()
{
	Solution s;
	ListNode* l1 = new ListNode(2);
	ListNode* l2 = new ListNode(3);
	l1->next = new ListNode(4);
	ListNode* l3 = s.mergeTwoLists(l1, l2);
	while (l3 != nullptr)
	{
		cout<< l3->val<< " ";
		l3 = l3->next;

	}
	return 0;
}
Example #26
0
int main()
{
    ListNode *a1 = new ListNode(1);
    ListNode *a2 = new ListNode(3);
    ListNode *a3 = new ListNode(6);
    ListNode *b1 = new ListNode(2);
    ListNode *b2 = new ListNode(4);
    ListNode *b3 = new ListNode(5);
    b1->next = b2;
    b2->next = b3;
    Solution s;
    a1 = s.mergeTwoLists(a1, b1);
    for(;a1; a1 = a1->next) cout<<a1->val<<" ";
    return 0;
}
int main()
{
    Solution s;
    ListNode *l1, *l2, *l3, *ptr;
    l1 = new ListNode(2);
    l2 = new ListNode(1);
    l3 = s.mergeTwoLists(l1,l2);
    ptr = l3;
    while(ptr)
    {
        cout<<ptr->val<<" ";
        ptr = ptr->next;
    }
    cout<<endl;
}
Example #28
0
// sumbit times(for the first accept): 1
int main(int argc, char *argv[])
{
    Solution s;
    ListNode *l1 = new ListNode(100);
    addNode(l1, 220);
    ListNode *l2 = new ListNode(100);
    addNode(l2, 150);
    addNode(l2, 200);
    addNode(l2, 250);
    addNode(l2, 300);
    ListNode *p = s.mergeTwoLists(l1, l2);
    print(p);

    return 0;
}
int main(int argc, char** argv) {
	Solution sol;
	char list1[] = "1 2 8 23 51 56 67";
	char list2[] = "3 4 5 9 34";
	ListNode* head1 = str2LinkedList(list1);
	ListNode* head2 = str2LinkedList(list2);
	ListNode* t= sol.mergeTwoLists(head1,head2);

	while(t!=NULL){
		cout<<t->val<<endl;
		t = t->next;
	}

	return 0;
}
static bool doTest(std::mt19937 &randomEngine,
                   std::uniform_int_distribution<> &randomSelectList,
                   Solution &solution,
                   std::vector<ListNode> *pVecNodes)
{
    std::uniform_int_distribution<> randomNodeCount(1, 5);

    ListNode *pNewHead1 = nullptr;
    ListNode *pNewHead2 = nullptr;
    ListNode *pNewTail1 = nullptr;
    ListNode *pNewTail2 = nullptr;
    int nodeCount = randomNodeCount(randomEngine);
    pVecNodes->resize(nodeCount);
    for (int i = 0; i < nodeCount; ++i)
    {
        ListNode *pNode = &pVecNodes->at(i);
        pNode->val = i;
        pNode->next = nullptr;

        bool bSelectList1 = randomSelectList(randomEngine);
        ListNode **ppTail = bSelectList1 ? &pNewTail1 : &pNewTail2;
        ListNode **ppHead = bSelectList1 ? &pNewHead1 : &pNewHead2;

        if (!*ppTail)
            *ppHead = pNode;
        else
            (*ppTail)->next = pNode;

        *ppTail = pNode;
    }

    ListNode *pRet = solution.mergeTwoLists(pNewHead1, pNewHead2);
    ListNode *pNode = pRet;
    auto itNode = pVecNodes->cbegin();
    while (pNode)
    {
        if (itNode == pVecNodes->cend())
            return false;

        if (pNode != &(*itNode))
            return false;

        pNode = pNode->next;
        ++itNode;
    }

    return (itNode == pVecNodes->cend());
}