zoukankan      html  css  js  c++  java
  • 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
    ****************************************************************/
    


    
    


    每天早上叫醒你的不是闹钟,而是心中的梦~
  • 相关阅读:
    中国科学技术大学2021年数学分析考研试卷
    中国科学技术大学2021年高等代数考研试卷
    中国海洋大学2021年数学分析考研试卷
    中国海洋大学2021年高等代数考研试卷
    中北大学2021年高等代数考研试卷
    数学分析教程第3版参考解答1.6自然对数的底
    数学分析中的问题与方法参考解答第1章极限论
    郑州大学2021年数学分析考研试卷
    郑州大学2021年高等代数考研试卷
    浙江师范大学2021年高等代数考研试卷
  • 原文地址:https://www.cnblogs.com/vintion/p/4116967.html
Copyright © 2011-2022 走看看