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
    ****************************************************************/
    


    
    


    每天早上叫醒你的不是闹钟,而是心中的梦~
  • 相关阅读:
    Unix / 类 Unix shell 中有哪些很酷很冷门很少用很有用的命令?(转)
    5分钟用Spring4 搭建一个REST WebService(转)
    一本好看的书————《反欺骗的艺术——世界传奇黑客的经历分享》
    国内外从事CV相关的企业
    定积分解法
    c++中&amp;和&amp;&amp;有什么差别
    树莓派相关-树莓派串口配置方法
    Linux下df与du两个命令的差别?
    veridata实验举例(2)验证表BONUS与表SALGRADE两节点同步情况
    Java替代C语言的可能性
  • 原文地址:https://www.cnblogs.com/vintion/p/4116967.html
Copyright © 2011-2022 走看看