LeetCode 707

 LeetCode 707 gdb没问题,但leetcode服务器跑出来老是不对

//C++ file
/*=============================================
*
*      Filename: Leetcode/7071.cpp
*
*        Author: Joe_CAO - flyinghrhino@163.com
*   Description: LeetCode 7071
*        Create: 2018-08-28 14:38:31
# Last Modified: 2018-08-28 14:38:46
=============================================*/

using namespace std; 
struct node
{ 
    int val; 
    node *next; 
}; 
class MyLinkedList { 
private: node *Head; 
public: /** Initialize your data structure here. */ 
         MyLinkedList() 
         { 
             Head = new node; 
             Head->val = -1; 
             Head->next =NULL; 
         } ~MyLinkedList(){ 
             node *p = Head->next; 
             while(p!= NULL) { 
                 node *q = p; 
                 p = p->next; 
                 delete q;
            }
            delete Head;
        }
        int get(int index) {
            if(index < 0)
                return -1;
            node *p = Head->next;
            node *q;
            int vv = 0;
            while(p!=NULL)
            {
                if (vv == index)
                {
                    return p->val;
                }
                else
                { 
                    vv++;
                    p=p->next;
                }
            }
            return -1;
        }

        /** Add a node of value val before the first element of the linked list.
         * After the insertion, the new node will be the first node of the
         * linked list. */
        void addAtHead(int val) {
            node *p = new node;
            p->val = val;
            p->next = Head->next;
            Head->next = p;
        }

        /** Append a node of value val to the last element of the linked
         * list. */
        void addAtTail(int val) {
            node *p = new node;
            node *q = Head->next;
            p->val = val;
            while(q != NULL)
                q = q->next;
            q = p;
        }

        /** Add a node of value val before the index-th node in the
         * linked list. If index equals to the length of linked list,
         * the node will be appended to the end of linked list. If index
         * is greater than the length, the node will not be inserted. */
        void addAtIndex(int index, int val) {
            node *p = new node;
            node *q = Head;
            node *t = q->next;
            p->val = val;
            int i = 0;
            while( i<index && t!= NULL)
            {
                q = q->next;
                t = t->next;
                i++;
            }
            if( i == index)
            {
                p->next = q->next;
                q->next = p;
            }

        }

        /** Delete the index-th node in the linked list, if the
         * index is valid. */
        void deleteAtIndex(int index) {
            node *q = Head;
            node *t = q->next;
            int i = 0;
            while( i<index && t!= NULL)
            {
                q = q->next;
                t = t->next;
                i++;
            }
            if( i == index && t!= NULL)
            {
                q->next = t->next;
                delete t;
            }
        }
};

int main(int argc, char* argv[])
{
    MyLinkedList linkedlist;
    linkedlist.addAtHead(1);
    //    cout<<linkedlist.get(0)<<endl;
    linkedlist.addAtTail(3);
    linkedlist.addAtTail(4);
    //    cout<<linkedlist.get(1)<<endl;
    linkedlist.addAtIndex(1,2);
    cout<<linkedlist.get(0)<<endl;
    cout<<linkedlist.get(1)<<endl;
    cout<<linkedlist.get(2)<<endl;
    //    cout<<linkedlist.get(3)<<endl;
    linkedlist.deleteAtIndex(1);
    cout<<linkedlist.get(0)<<endl;
    cout<<linkedlist.get(1)<<endl;
    cout<<linkedlist.get(2)<<endl;
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值