**203.移除連結串列元素****707.設計連結串列****206.反轉連結串列**

晴夜空發表於2024-06-25

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 ;








}
};

相關文章