1、LeetCode203 移除链表元素
题目链接:203、移除链表元素
首先定义虚拟头结点 ListNode * dummyHead = new ListNode(0); dummyHead->next = head;
用一个中间变量在链表中移动,首先将它指向虚拟头结点 ListNode * cur = dummyHead;
当cur->next是要删除的元素时,cur->next = cur->next->next, 并释放要删除元素的内存;
最后释放虚拟头结点的内存。
/**
* 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 * dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode * cur = dummyHead;
while (cur->next != NULL)
{
if (cur->next->val == val)
{
ListNode *temp = cur->next;
cur->next = cur->next->next;
delete temp;
}
else
{
cur = cur->next;
}
}
head = dummyHead->next;
delete dummyHead;
return head;
}
};
2、LeetCode707 设计链表
题目链接:707、设计链表
class MyLinkedList {
public:
struct LinkNode
{
int val;
LinkNode * next;
LinkNode(int val):val(val),next(NULL){}
};
MyLinkedList() {
_dummyHead = new LinkNode(0);
_size = 0;
}
int get(int index) {
if (index > (_size-1) || index < 0)
{
return -1;
}
LinkNode * cur = _dummyHead->next;
while(index--)
{
cur = cur->next;
}
return cur->val;
}
void addAtHead(int val) {
LinkNode * newNode = new LinkNode(val);
newNode->next = _dummyHead->next;
_dummyHead->next = newNode;
_size++;
}
void addAtTail(int val) {
LinkNode * newNode = new LinkNode(val);
LinkNode * cur = _dummyHead;
while(cur->next!=NULL)
{
cur = cur->next;
}
cur->next = newNode;
_size++;
}
void addAtIndex(int index, int val) {
if (index > _size )
{
return ;
}
if (index < 0)
{
index = 0;
}
LinkNode * newNode = new LinkNode(val);
LinkNode * cur = _dummyHead;
while(index--)
{
cur = cur->next;
}
newNode->next = cur->next;
cur->next = newNode;
_size++;
}
void deleteAtIndex(int index) {
if( index > (_size-1) || index < 0)
{
return;
}
else
{
LinkNode * cur = _dummyHead;
while(index--)
{
cur = cur->next;
}
LinkNode * temp = cur->next;
cur->next = cur->next->next;
delete temp;
_size--;
}
}
private:
int _size;
LinkNode * _dummyHead;
};
/**
* 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);
*/
3、反转链表
题目链接:206、反转链表
思想:双指针,cur指向头结点,pre为NULL;在遍历时,temp指向cur->next,cur->next = pre,pre = cur,cur = temp;
/**
* 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 * temp;
ListNode * cur = head;
ListNode * pre = NULL;
while (cur)
{
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
};
递归法
/**
* 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 * reverse(ListNode * pre, ListNode * cur)
{
if (cur == NULL)
{
return pre;
}
ListNode * temp = cur->next;
cur->next = pre;
return reverse(cur, temp);
}
ListNode* reverseList(ListNode* head) {
return reverse(NULL,head);
}
};