leetcode 21. Merge Two Sorted Lists

来源:互联网 发布:盈建科软件最新版下载 编辑:程序博客网 时间:2024/05/17 01:49

每个链表一个游标,把小的数插入结果就行了。

#include <stdio.h>#include <stdlib.h>#include <stddef.h>struct ListNode* add(struct ListNode* l1,int num);struct ListNode* removeNthFromEnd(struct ListNode* head, int n);struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2);int print(struct ListNode* l1); struct ListNode {     int val;     struct ListNode *next; };int main(){    struct ListNode *l1=NULL;    struct ListNode *l2=NULL;    struct ListNode *merged=NULL;    l1=add(l1,1);    add(l1,4);    add(l1,6);    l2=add(l2,2);    add(l2,4);    add(l2,9);    add(l2,10);    print(l1);    print(l2);    //result=addTwoNumbers()    printf("-------------------\n");    //l1=removeNthFromEnd(l1,2);    merged=mergeTwoLists(l1,l2);    print(merged);    return 0;}struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {    struct ListNode* pointer1=l1;    struct ListNode* pointer2=l2;    struct ListNode* merged=NULL;    struct ListNode* temp=NULL;    struct ListNode* mergedPointer=NULL;    while(pointer1!=NULL||pointer2!=NULL)    {        if(pointer1!=NULL&&pointer2!=NULL)        {            if(pointer1->val<=pointer2->val)            {                temp=(struct ListNode*)malloc(sizeof(struct ListNode));                temp->next=NULL;                temp->val=pointer1->val;                pointer1=pointer1->next;            }            else            {                temp=(struct ListNode*)malloc(sizeof(struct ListNode));                temp->next=NULL;                temp->val=pointer2->val;                pointer2=pointer2->next;            }        }        else if(pointer1!=NULL)        {            temp=(struct ListNode*)malloc(sizeof(struct ListNode));            temp->next=NULL;            temp->val=pointer1->val;            pointer1=pointer1->next;        }        else        {            temp=(struct ListNode*)malloc(sizeof(struct ListNode));            temp->next=NULL;            temp->val=pointer2->val;            pointer2=pointer2->next;        }        if(merged==NULL)        {            merged=temp;            mergedPointer=temp;        }        else        {            mergedPointer->next=temp;            mergedPointer=temp;        }    }    return merged;}struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {    struct ListNode* pointer=head;    int i,count;    i=0;    count=0;    if(pointer->next==NULL&&n==1)        return NULL;    while(pointer!=NULL)    {        pointer=pointer->next;        count++;    }    pointer=head;    if(n==count)    {        head=head->next;        return head;    }    while(pointer->next!=NULL)    {        if(i==count-n-1)            break;        pointer=pointer->next;        i++;    }    pointer->next=pointer->next->next;    return head;}int print(struct ListNode* l1){    struct ListNode* pointer;    pointer=l1;    while(pointer!=NULL)    {        printf("%d ",pointer->val);        pointer=pointer->next;    }    return 0;}struct ListNode* add(struct ListNode* l1,int num){    struct ListNode* pointer;    struct ListNode* newNode;    pointer=l1;    if(l1==NULL)    {        newNode=(struct ListNode*)malloc(sizeof(struct ListNode));        newNode->val=num;        newNode->next=NULL;        l1=newNode;    }    else    {        while(pointer->next!=NULL)        {            pointer=pointer->next;        }        newNode=(struct ListNode*)malloc(sizeof(struct ListNode));        newNode->val=num;        newNode->next=NULL;        pointer->next=newNode;    }    return l1;}/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; *//*struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {}*/

0 0