**203.移除链表元素****707.设计链表****206.反转链表**

203.移除链表元素**707.设计链表206.反转链表**

 

203.移除链表元素

题目地址 : https://leetcode.cn/problems/remove-linked-list-elements/submissions/541762363/

代码 :

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
   ListNode* removeElements(ListNode* head, int val) {

       ListNode* p;
       ListNode* pre;

       ListNode* LinkedList_Return;

       p = head ;

       LinkedList_Return = nullptr;


       pre = p;

       while( p != nullptr )
      {
           

           if(p->val == val)
          {
               pre->next = p->next;

               pre = pre ;

          }
           else if(LinkedList_Return == nullptr)
          {
               LinkedList_Return = p;

               pre = p;            // 要 区分 情况    
          }
           else        // 注意 情况 分类
          {
               pre = p;

          }

           

           p = p->next;
      }



       return LinkedList_Return ;


  }
};

 

 

 

707.设计链表

题目地址 : https://leetcode.cn/problems/design-linked-list/

代码 :

class MyLinkedList {
public:

   struct LinkedList
  {
       int val;
       LinkedList * next;

  };

   LinkedList * head = nullptr ;

   int length_LinkedList;


   MyLinkedList() {
       length_LinkedList = 0;

  }
   
   int get(int index) {
       LinkedList * p ;

       p = head ;

       int count_Index = 0;


       if(index < 0 || index >= length_LinkedList)
      {
           return -1 ;

      }

       while( p != nullptr && count_Index < index )
      {

           
           
           p = p->next;

           count_Index++;

      }


       if(count_Index < index )
      {
           return -1;

      }

       return p->val ;


  }
   
   void addAtHead(int val) {

       if(head == nullptr)
      {
           LinkedList * temp = (LinkedList *) malloc (sizeof (LinkedList));

           temp->val = val;
           temp->next = nullptr;

           head = temp;

      }
       else
      {
           LinkedList * p = head;

           LinkedList * temp = (LinkedList *) malloc (sizeof (LinkedList));

           temp->val = val;
           temp->next = head;

           head = temp;


      }

       length_LinkedList++ ;

  }
   
   void addAtTail(int val) {


       if(head == nullptr)
      {
           LinkedList * temp = (LinkedList *) malloc (sizeof (LinkedList));

           temp->val = val;
           temp->next = nullptr;

           head = temp;

      }
       else
      {
           LinkedList * p = head;

           LinkedList * temp = (LinkedList *) malloc (sizeof (LinkedList));

           temp->val = val;
           temp->next = nullptr;


           while(p->next != nullptr )
          {

               p = p->next ;

          }

           p->next = temp ;


      }

       length_LinkedList++ ;

  }
   
   void addAtIndex(int index, int val) {

       int count_Index;


       LinkedList * p = head ;

       LinkedList * pre = nullptr ;



       count_Index = 0;

       //cout << "length_LinkedList : "<<length_LinkedList<<endl;

       if(index < 0 || index > length_LinkedList)
      {
           return ;


      }

       if( index == 0 )
      {
           addAtHead(val);

           return ;

      }
       else if( (index ) == length_LinkedList)
      {
           addAtTail(val);

           return ;


      }


       while( p != nullptr && count_Index < index )
      {

           pre = p;

           p = p->next ;

           count_Index++;

      }

       if(count_Index < index )
      {
           cout<<" 输入 的 index 超出 了 链表 长度 " << endl ;   // 在 其它 函数 部分 正常 工作   时        

      }


       LinkedList * temp = (LinkedList *) malloc (sizeof (LinkedList));

       temp->val = val;
       temp->next = p ;


       pre->next = temp;

       length_LinkedList++ ;




  }
   
   void deleteAtIndex(int index) {
       
       int count_Index;

       LinkedList * p = head ;

       LinkedList * pre = nullptr ;


       count_Index = 0;

       if(index < 0 || index >= length_LinkedList)
      {
           return ;

      }


       if( index == 0 )
      {
           head = p->next ;

           length_LinkedList-- ;

           return ;

      }
       else if( (index + 1 ) == length_LinkedList )
      {
           
           while(p->next != nullptr )
          {
               pre = p;
               p = p->next;

          }

           pre->next = nullptr;

           length_LinkedList-- ;

           return ;


      }

       //cout<< "p->val : " << p->val << endl;

       while( p != nullptr && count_Index < index )
      {

           pre = p ;

           //cout<< "p->val : " << p->val << endl;

           p = p->next ;

           count_Index++;

           //cout<<"line"<<endl;

      }

       //cout<< "p->val : " << p->val << endl;

       //if(pre == nullptr)
       //{
       //   cout<< 1 << endl;
       //}

       //if(p == nullptr)
       //{
       //   cout<< 2 << endl;
       //}

       pre->next = p->next ;


       length_LinkedList-- ;







  }
};

/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList* obj = new MyLinkedList();
* int param_1 = obj->get(index);
* obj->addAtHead(val);
* obj->addAtTail(val);
* obj->addAtIndex(index,val);
* obj->deleteAtIndex(index);
*/

 

 

206.反转链表

题目地址 : https://leetcode.cn/problems/reverse-linked-list/

代码 : (”头插法“)

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
   ListNode* reverseList(ListNode* head) {
       ListNode* p1;
       ListNode* p2 = nullptr;
       ListNode* ptr_NewLinkedListHead = nullptr;

       ListNode* next = nullptr;

       ListNode* Node_Temp = nullptr;
       

       p1 = head;

       while( p1 != nullptr )
      {

           next = p1 -> next ;

           if(p2 == nullptr )
          {
               Node_Temp = p1;

               // 末尾 指针 的 清理    

               Node_Temp -> next = nullptr ;


               p2 = Node_Temp ;

               ptr_NewLinkedListHead = p2;



          }
           else
          {
               Node_Temp = p1;


               Node_Temp -> next = ptr_NewLinkedListHead;


               ptr_NewLinkedListHead = Node_Temp;



          }


           p1 = next;

      }

       return ptr_NewLinkedListHead ;


  }
};

 

代码 : (“使用 Stack 的 支持” )

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
   ListNode* reverseList(ListNode* head) {

       // 使用 栈   的   support     ?


       ListNode* p1 = head;

       ListNode* p2 = nullptr ;


       ListNode* ptr_NewLinkedListHead = nullptr;


       stack<int> stack_Temp;



       while(p1 != nullptr )
      {

           stack_Temp.push(p1->val);

           //cout<< 1 << endl;
           //cout<< stack_Temp.top() << endl;


           p1 = p1->next ;

      }



       //while( stack_Temp.size() != 0)
       while( stack_Temp.empty() == 0)
      {
           //cout<< stack_Temp.top() << endl;

           int num_Temp = stack_Temp.top();

           stack_Temp.pop();

           //ListNode* Node_Temp = (ListNode*)malloc(sizeof(ListNode));

           ListNode* Node_Temp = new ListNode(num_Temp);

           Node_Temp->val = num_Temp ;

           Node_Temp->next = nullptr ;

           // 分 情况                


           if(p2 == nullptr)
          {
               p2 = Node_Temp;

               ptr_NewLinkedListHead = p2 ;
          }
           else
          {
               p2->next = Node_Temp;

               p2 = p2->next ;

          }

           


      }

       //cout<< 1111 << endl;

       //ListNode* p3 = ptr_NewLinkedListHead;

       //while(p3 != nullptr )
       //{
           //cout<<p3->val<<endl;

           //p3 = p3->next;
       //}

       return ptr_NewLinkedListHead ;








  }
};
 

 

 

 

posted @ 2024-06-25 02:03  晴夜空  阅读(6)  评论(0)    收藏  举报