两数相加
环形链表 II
相交链表
删除排序链表中的重复元素 II
分析:
初始化一个结点作为头结点,指向head,cur指向当前结点,初始为head。如果当前cur(s)和cur->next指向结点的值相同,说明遇到重复值,需要删除;记要删除的值为x,后移cur,保证cur->next->val == x;从s到cur->next都是需要删除的结点;由于cur后移,所以已经删除成功!
代码:
/**
* 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* deleteDuplicates(ListNode* head) {
if(!head) {
return NULL;
}
ListNode* ptr = new ListNode(0, head);
ListNode* cur = ptr;
while(cur->next && cur->next->next) {
if(cur->next->val == cur->next->next->val) {
int x = cur->next->val;
while(cur->next && cur->next->val == x) {
cur->next = cur->next->next;
}
}else {
cur = cur->next;
}
}
return ptr->next;
}
};
两两交换链表中的节点
设计链表
分析:
带有头尾指针的单链表:
- 初始化:初始化一个头结点p,然后令head和tail都为p
- 头部添加:添加后如果len == 1,说明添加前还是空链表,要令tail = head->next。
- 尾部添加:添加后如果len == 1,要令head->next = tail
- 删除:如果删除前len == 1,删除后要令head->next = tail->next = NULL;如果删除的是最后一个结点,删除后要对tail重新赋值
代码:
class MyLinkedList {
private:
struct node {
int val;
node* next;
node(int x):val(x), next(NULL){}
};
node* p = new node(0); //头结点
node* head = p;
node* tail = p;
int len = 0; //链表长度
public:
/** Initialize your data structure here. */
MyLinkedList() {
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
int get(int index) {
if(index >= len) {
return -1;
}
node* ptr = head->next;
for(int i = 0; i < index; i++) {
ptr = ptr->next;
}
return ptr->val;
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
void addAtHead(int val) {
node* ans = new node(val);
ans->next = head->next;
head->next = ans;
if(len == 0) {
tail = ans;
}
len++;
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
node* ans = new node(val);
tail->next = ans;
tail = ans;
len++;
if(head->next == NULL) {
head->next = tail;
}
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
void addAtIndex(int index, int val) {
if(index > len) {
return;
}
if(index <= 0) {
addAtHead(val);
return;
}
if(index == len) {
addAtTail(val);
return;
}
node* ptr = head->next;
for(int i = 0; i < index - 1; i++) {
ptr = ptr->next;
}
node* ans = new node(val);
ans->next = ptr->next;
ptr->next = ans;
len++;
}
void print() {
node* ptr = head->next;
if(ptr == NULL) {
cout << "empty" << endl;
return;
}
while(ptr) {
cout << ptr->val << " --> ";
ptr = ptr->next;
}
cout << endl;
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
node* t = head->next;
int cnt = 0;
if(index < 0 || index >= len) {
return;
}
if(index == 0) {
head->next = head->next->next;
if(len == 1) {
tail = head;
}
len--;
return;
}
node* ptr = head->next;
for(int i = 0; i < index - 1; i++) {
ptr = ptr->next;
}
node* q = ptr->next;
ptr->next = q->next;
if(index == len - 1) {
tail = ptr;
}
len--;
}
};
/**
* 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);
*/