leetcode初級アルゴリズムチェーンテーブル


チェーンテーブル
  • チェーンテーブルのノード
  • を削除する.
  • チェーンテーブルの最後からN番目のノード
  • を削除する.
  • 反転チェーンテーブル
  • は、2つの秩序チェーンテーブル
  • を結合する.
  • 回文チェーン表
  • huanxingチェーンテーブル
  • チェーンテーブルのノードの削除
    テーマ:関数の唯一のパラメータは削除するノードのポインタであり、指すノードは絶対に最後ではありません.考え方:自分が最初に頭の針をどのように操作していないか分からないと思って、他の人の考えを見てやっと分かって、移動するだけでいいです.ACコード:
    class Solution {
    public:
        void deleteNode(ListNode* node) {
            // node         
            ListNode* tail;
            while(node->next!=NULL)
            {
                tail = node;
                node->val = node->next->val;
                node = node->next;
            }
            tail->next = NULL;
            delete(node);
        }
    };
    

    チェーンテーブルの最後からN番目のノードを削除
    ACコード:
    class Solution {
    public:
        ListNode* removeNthFromEnd(ListNode* head, int n) {
           stack p;
            ListNode* cur = head;
            while(cur!=NULL)
            {
                p.push(cur);
                cur = cur->next;
            }
            for(int i = 0;inext;
                return head;}
            else{
            cur = p.top();
            cur->next = cur->next->next;
            return head;}
        }
    };
    

    チェーンテーブルを反転
    cuowudaima
    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            if(head==NULL||head->next==NULL)
                return head;    //          ,   reverse 
            else
            {
                //
                ListNode* p = head,*a;
                while(p->next!=NULL)
                {
                    a=p;
                    p=p->next;
                }
                a->next = NULL;
                p->next = head;
                head = p;
                reverseList(head->next);
                return head;
            }
        }
    };
    

    AC
    public:
        ListNode* reverseList(ListNode* head) {
            if(head==NULL||head->next==NULL)
                return head;    //          ,   reverse 
            else
            {
                //
                ListNode* p = head,*a;
                while(p->next!=NULL)
                {
                    a=p;
                    p=p->next;
                }
                a->next = NULL;
                p->next = head;
                head = p;
                head->next=reverseList(head->next);
                return head;
            }
        }
    };
    

    2つの順序付きチェーンテーブルを結合
    cuowudaima:
    class Solution {
    public:
        ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
            if(l1==NULL&&l2==NULL) return NULL;
            ListNode* last;
            ListNode* head = (ListNode*)malloc(sizeof(ListNode)),*p=head;
            while(l1!=NULL&&l2!=NULL)
            {
                ListNode* cur = (ListNode*)malloc(sizeof(ListNode));
                if(l1->valval)
                {
                    p->val = l1->val;
                    l1 = l1->next;
                }
                else
                {
                    p->val = l2->val;
                    l2 = l2->next;
                }
                last = p;
                p->next = cur;
                p = cur;
            }
            if(l1==NULL)
            {
                while(l2!=NULL)
                {
                    ListNode* cur = (ListNode*)malloc(sizeof(ListNode));
                    p->val = l2->val;
                    l2 = l2->next;
                    last = p;
                    p->next = cur;
                    p = cur;
                }
            }
            else
            {
                 while(l1!=NULL)
                {
                    ListNode* cur = (ListNode*)malloc(sizeof(ListNode));
                    p->val = l1->val;
                    l1 = l1->next;
                     last = p;
                    p->next = cur;
                    p = cur;
                }
            }
            free(last->next);
            last->next = NULL;
            return head;
        }
    };
    

    AC(recursion)
    class Solution {
    public:
        ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
            if(l1==NULL&&l2==NULL) return NULL;
            if(l1==NULL) return l2;
            if(l2==NULL) return l1;
            ListNode* temp;
            if(l1->valval)
            {
                temp = l1;
                l1=l1->next;
                temp->next = mergeTwoLists(l1,l2);
                return temp;
            }
            else
            {
                temp = l2;
                l2=l2->next;
                temp->next = mergeTwoLists(l1,l2);
                return temp;
            }
            return NULL;    //for passing compiling only 
        }
    };
    

    エコーチェーン
    yuanli:make use of a stack and a queue. tongguodaima:
    class Solution {
    public:
        bool isPalindrome(ListNode* head) {
            stack back;
            queue front;
            int cnt = 0;
            while(head!=NULL)
            {
                cnt++;
                back.push(head->val);
                front.push(head->val);
                head = head->next;
            }
            int x = cnt/2;     //if 6 items,then check 3 times.if 5 items,check 2 times
            while(x--)
            {
                int v = back.top();
                int u = front.front();
                if(v!=u) return false;
                back.pop();
                front.pop();
            }
            return true;
        }
    };
    

    义齿
    cuowu
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            set visited;
            while(head!=NULL)
            {
                if(visited.find(head)!=visited.end()) {return false;}
                visited.insert(head);
                head = head->next;
            }
            return true;
        }
    };
    

    bierende:
    class Solution {
    public:
        bool hasCycle(ListNode *head) {
            if(head==NULL||head->next==NULL) return false;
            if(head->next==head) return true;
            ListNode* t=head->next;
            head ->next = head;
            return hasCycle(t);
        }
    };