jobdu 1518 1517

#include <iostream>
using namespace std;
 
struct LinkNode
{
    int val;
    LinkNode *next;
    LinkNode(int x):val(x),next(NULL){};
};
 
LinkNode *initList(int n)
{
   if(n<=0) 
        return NULL;
    LinkNode *root = NULL;
    LinkNode *p, *q;
    int value;
    while(n--)
    {
        cin>>value;
        p = new LinkNode(value);
        if(!root)
        {
            root = p;
            q = root;
            continue; //if not, when n =1, it's a loop;
        }
        q->next = p;
        q = q->next;
    }
    return root;
}
 
LinkNode *reverse(LinkNode *root)
{
    if(!root)
        return NULL;
    LinkNode *p, *q, *temp;
    p = root;
    q = root->next;
 
    while(q)
    {
        temp = q;
        q = q->next;
        temp->next = p;
        p = temp;
    }
    root->next = NULL;
    return p;
}
 
void printList(LinkNode *root)
{
    while(root)
    {
        cout<<root->val;
        root = root->next;
        if(root)
            cout<<' ';
    }
    cout<<endl;
}
 
int main()
{
    LinkNode *root,*newRoot;
    int n;
    while(cin>>n)
    {
        if(n==0)
        {
            cout<<"NULL"<<endl;
            continue;
        }
        root = initList(n);
        printList(root);
        newRoot = reverse(root);
        printList(newRoot);
    }
    return 0;
}
 
/**************************************************************
    Problem: 1518
    User: zhongky
    Language: C++
    Result: Wrong Answer
****************************************************************/
#include <iostream>
using namespace std;
 
struct LinkNode
{
    int val;
    LinkNode *next;
    LinkNode(int x):val(x),next(NULL){};
};
 
LinkNode *initList(int n)
{
   if(n<=0) 
        return NULL;
    LinkNode *root = NULL;
    LinkNode *p, *q;
    int value;
    while(n--)
    {
        cin>>value;
        p = new LinkNode(value);
        if(!root)
        {
            root = p;
            q = root;
            continue; //if not, when n =1, it's a loop;
        }
        q->next = p;
        q = q->next;
    }
    return root;
}
 
LinkNode *findKthNode(LinkNode *root, int n, int k)
{
    //three cases:
    if(!root)    
        return NULL; 
    if(k>n||k==0)
        return NULL;
    if(n==k)
        return root;
 
    LinkNode *p,*q;
    int i = 0;
    p = root;
    q = root;
    while(++i!=k)
        q = q->next;
    while(q->next) //q->next
    {
        q = q->next;
        p = p->next;
    }
    return p;
}
 
int main()
{
    int n,k;
    LinkNode *root;
    LinkNode *p;
    while(cin>>n>>k)
    {
        root = initList(n);
        p = findKthNode(root,n,k);
        if(!p)
        {
            cout<<"NULL"<<endl;
            continue;
        }
        cout<<p->val<<endl;
    }
    return 0;
}
 
/**************************************************************
    Problem: 1517
    User: zhongky
    Language: C++
    Result: Accepted
    Time:200 ms
    Memory:3104 kb
****************************************************************/




posted @ 2013-12-19 21:57  海滨银枪小霸王  阅读(110)  评论(0编辑  收藏  举报