OJ-链表

来源:互联网 发布:淘宝上买的东西靠谱吗 编辑:程序博客网 时间:2024/05/16 07:23
**1. 有一个整数val,如何在节点值有序的环形链表中插入一个节点值为val的节点,并且保证这个环形单链表依然有序。

给定链表的信息,及元素的值A及对应的nxt指向的元素编号同时给定val,请构造出这个环形链表,并插入该值。
测试样例:
[1,3,4,5,7],[1,2,3,4,0],2
返回:{1,2,3,4,5,7}**

/*struct ListNode {    int val;    struct ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};*/class InsertValue {public:    typedef ListNode* node;    ListNode* insert(vector<int> A, vector<int> nxt, int val) {        // write code here        ListNode* ret;        if (A.size() > 1)        {            node valNode = new ListNode(val);            ret = initList(A,nxt);            if (val <= ret->val)            {                valNode-> next = ret;                ret = valNode;                return ret;            }            node curr = ret;            node pos  = ret-> next;            while (pos != NULL)            {                if (val >= curr->val && val <= pos->val)//                {                    curr-> next = valNode;                    valNode-> next = pos;                    return ret;                }                //                curr = curr-> next;                pos  = pos-> next;            }            curr-> next = valNode;            valNode-> next = pos;            return ret;        }        if (A.size() == 1)        {            node valNode = new ListNode(val);            node tempNode = new ListNode(A[0]);            node head = (val <= A[0]) ? valNode                                      : tempNode;            head->next = (val <= A[0])? tempNode                                      : valNode;                                    return head;        }        //A is empty        return new ListNode(val);    }    ListNode* initList( vector<int> &A,vector<int> &nxt){        ListNode* head = new ListNode(A[0]);        node ptr = head;        for (std::size_t i = 1; i < A.size(); ++i)        {            ptr -> next = new ListNode(A[i]);            ptr = ptr -> next;        }        return head;    }};
2 . 实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点。给定带删除的节点,请执行删除操作,若该节点为尾节点,返回false,否则返回true.
/*struct ListNode {    int val;    struct ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};*/class Remove {public:    bool removeNode(ListNode* pNode) {        // write code here        if (NULL == pNode-> next)        {            return false;        }        ListNode* nextNode = pNode->next;        pNode-> val = nextNode->val;        pNode->next = nextNode->next;        delete nextNode;        return true;    }};

3.现有两个升序链表,且链表中均无重复元素。请设计一个高效的算法,打印两个链表的公共值部分。给定两个链表的头指针headA和headB,请返回一个vector,元素为两个链表的公共部分。请保证返回数组的升序。两个链表的元素个数均小于等于500。保证一定有公共值

测试样例:
{1,2,3,4,5,6,7},{2,4,6,8,10}
返回:[2.4.6]

class Common {public:    vector<int> findCommonParts(ListNode* headA, ListNode* headB) {        // write code here        vector<int> ret;                ListNode *currA = headA, *currB = headB;        while ( !isEmpty(currA) && !isEmpty(currB))        {            if (currA->val > currB->val)            {                currB = front(currB);            }else if (currA-> val < currB->val){                currA = front(currA);            }else{//equals                ret.push_back(currA->val);                currA = front(currA);                currB = front(currB);            }        }        return ret;    }    bool isEmpty(ListNode *node)    {        return nullptr == node;    }    ListNode *front(ListNode *ptr){        return ptr-> next;    }};
0 0
原创粉丝点击