程序代码:

#include<stdio.h>
#include<stdlib.h>
struct ListNode
{
    int data;
    struct ListNode* next;
};
typedef struct ListNode node;
int main()
{
    int i;
    node l1,l2,l3;
    l1.next=NULL;
    l2.next = NULL;
    l3.next = NULL;
    node* p = &l1;
    char c;
    while((scanf("%d",&i))&&i!=-1)
    {
        node* s = (node*)malloc(sizeof(node));
        s->data = i;
        s->next = NULL;
        p->next=s;
        p=p->next;      
    }
    p=&l2;
    while((scanf("%d",&i))&&i!=-1)
        {
                node* s = (node*)malloc(sizeof(node));
                s->data = i;
                s->next = NULL;
                p->next=s;
                p=p->next;
        }
    p=l1.next;
    node *q=l2.next;
    node *m = &l3;
    while((p!=NULL)&&(q!=NULL))
    {
        if((p->data)<(q->data))
        {
            p=p->next;
        }
        else if((p->data)>(q->data))
        {
            q=q->next;
        }
        else if((p->data)==(q->data))
        {
            node* s = (node*)malloc(sizeof(node));
            s->data = p->data;
            s->next = NULL;
            m->next = s;
            m= m->next; 
            p = p->next;
            q = q->next;
        }
    }
    m=l3.next;
    if(l3.next==NULL)
    {   printf("NULL");return 0;}

    while(m!=NULL)
    {
        if(m->next==NULL)
            printf("%d",m->data);
        else 
            printf("%d ",m->data);
        m=m->next;
    }
    return 0;
}

程序运行结果: