鏈表面試題練習一(力扣leetcode、牛客)

鏈表面試題練習二
1.刪除鏈表中等於給定值 val 的所有節點

class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        ListNode* newnode=new ListNode(0);
        newnode->next=head;
        ListNode* pCur=newnode;
        while(pCur->next!=nullptr)
        {
            if(pCur->next->val==val)
            {
                pCur->next=pCur->next->next;
            }
            else
                pCur=pCur->next;
        }
        return newnode->next;
    }
};

2. 反轉一個單鏈表

class Solution {
public:
    ListNode* reverseList(ListNode* head) {
        ListNode *pre=nullptr;
        ListNode *cur=head;
        ListNode *next=nullptr;
        while(cur!=nullptr){
            next=cur->next;
            cur->next=pre;
            pre=cur;
            cur=next;
        }
        return pre;
    }

3. 給定一個帶有頭結點 head 的非空單鏈表,返回鏈表的中間結點。
如果有兩個中間結點,則返回第二個中間結點

//快慢節點做法
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
      ListNode* slow=head;
      ListNode* quick=head;
      while(quick!=nullptr&&quick->next!=nullptr)
      {
        slow=slow->next;
        quick=quick->next->next;
      }
      return slow;
    }
};

4. 輸入一個鏈表,輸出該鏈表中倒數第k個結點

class Solution {
public:
    ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) {
      if(pListHead == nullptr||k==0)
           return nullptr;
      ListNode* knode=pListHead;
      ListNode* node=pListHead;
      for(int i =1;i<k;i++)
      {
        if(knode->next!=nullptr)
            knode=knode->next;
        else
            return nullptr;
      }
      while(knode->next!=nullptr)
      {
        node=node->next;
        knode=knode->next;
      }
      return node;
    }
};

5. 將兩個有序鏈表合併爲一個新的有序鏈表並返回。新鏈表是通過拼接給定的兩個鏈表的所有節點組成的

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* n1=l1;
        ListNode* n2=l2;
        ListNode* node=new ListNode(0);
        ListNode* end=node;
        while(n1&&n2)
        {
            if(n1->val<n2->val)
            {
                end->next=n1;
                end=end->next;
                n1=n1->next;
            }
            else
            {
                end->next=n2;
                end=end->next;
                n2=n2->next;
            }
        }
        //兩條鏈表不能同時爲空,必有一條不爲空
        if(n1!=nullptr)
            end->next=n1;
        else
            end->next=n2;
        return node->next;
    }
};

6.編寫代碼,以給定值x爲基準將鏈表分割成兩部分,所有小於x的結點排在大於或等於x的結點之前

class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        ListNode *small=new ListNode(0);
        ListNode *big=new ListNode(0);
        ListNode* small_end=small;
        ListNode* big_end=big;
        while(pHead){
            if(pHead->val<x){
                small->next=pHead;
                small=small->next;
            } else {
                big->next=pHead;
                big=big->next;
            }
            pHead=pHead->next;
        }
        big->next=nullptr;
        small->next=big_end->next;
        return small_end->next;
 
    }
};

7. 在一個排序的鏈表中,存在重複的結點,請刪除該鏈表中重複的結點,重複的結點不保留,返回鏈表頭指針

class Solution {
public:
    ListNode* deleteDuplication(ListNode* pHead)
    {
      ListNode* newHead=new ListNode(0);
      newHead->next=pHead;
      ListNode* pre=newHead;
      ListNode* pnode=pHead;
      ListNode* pCur=NULL;
      while(pnode!=NULL && pnode->next!=NULL)
      {
        pCur=pnode->next;
        if(pnode->val==pCur->val)
        {
          while(pCur!=NULL && pCur->val==pnode->val)
            pCur=pCur->next;
          pre->next=pCur;
          pnode=pCur;
        }
        else
        {
          pre=pnode;
          pnode=pnode->next;
        }
      }
      return newHead->next;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章