窝已经菜到双链表都要写个博客了。。。
设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val
和 next
。val
是当前节点的值,next
是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev
以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。
在链表类中实现这些功能:
- get(index):获取链表中第
index
个节点的值。如果索引无效,则返回-1
。 - addAtHead(val):在链表的第一个元素之前添加一个值为
val
的节点。插入后,新节点将成为链表的第一个节点。 - addAtTail(val):将值为
val
的节点追加到链表的最后一个元素。 - addAtIndex(index,val):在链表中的第
index
个节点之前添加值为val
的节点。如果index
等于链表的长度,则该节点将附加到链表的末尾。如果index
大于链表长度,则不会插入节点。如果index
小于0,则在头部插入节点。 - deleteAtIndex(index):如果索引
index
有效,则删除链表中的第index
个节点。
用数组模拟双链表,相比于动态分配内存和指针操作要快很多(因为new操作耗时较多),建立两个额外的节点head与tail表示链表头尾。
const int N = 1010;
class MyLinkedList {
public:
int e[N],l[N],r[N],idx; //e表示值,l表示左指针,r表示右指针,idx是当前第几个操作的数(并非链表的第几个元素)
int len; //表示链表长度
//初始化
MyLinkedList() {
r[0] = 1, l[1] = 0; //设0和1分别为头、尾节点
idx = 2; //idx从2开始,因为0和1已经用了两个
len = 0;
}
//输出链表的每个元素
void show()
{
for (int i = r[0]; i != 1; i = r[i]) cout << e[i] << ' ';
cout << endl;
}
//在第a个数的后面插入元素x,此处a和idx表示的含义相同
void insert(int a, int x)
{
e[idx] = x;
l[idx] = a, r[idx] = r[a];
l[r[a]] = idx, r[a] = idx ++ ;
len++;
}
/** 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;
//找到链表的第index个元素,从头节点的右节点开始遍历
int i = r[0], j=0;
for (i = r[0]; i != 1 && j<index; i = r[i]) j++;
return e[i];
}
/** 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) {
insert(0,val);
}
/** Append a node of value val to the last element of the linked list. */
void addAtTail(int val) {
insert(l[1],val);
}
/** 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)
{
addAtTail(val);
return ;
}
if(index>len) return ;
int i=0,j=0;
for (i = 0; i != 1 && j<index; i = r[i]) j++;
insert(i,val);
}
/** Delete the index-th node in the linked list, if the index is valid. */
void deleteAtIndex(int index) {
if(index>=len) return ;
int j = 1, i;
for (i = r[0]; i != 1 && j<=index; i = r[i]) j++;
l[r[i]] = l[i];
r[l[i]] = r[i];
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);
*/
快慢指针操作
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head==NULL) return false;
ListNode *a = head;
ListNode *b = head->next;
while(a!=b)
{
if(b==NULL || b->next==NULL) return false;
a = a->next;
b = b->next->next;
}
return true;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head==NULL) return NULL;
ListNode *a = head;
ListNode *b = head->next;
while(a!=b)
{
if(b==NULL || b->next==NULL) return NULL;
a = a->next;
b = b->next->next;
}
int res = 1;
a = a->next;
b = b->next->next;
while(a!=b)
{
a = a->next;
b = b->next->next;
res++;
}
a = head;
b = head;
for(int i=0;i<res;i++) a = a->next;
while(a!=b)
{
a = a->next;
b = b->next;
}
return a;
}
};
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode *A = headA;
ListNode *B = headB;
int lenA = 0;
int lenB = 0;
while(A!=NULL)
{
A = A->next;
lenA++;
}
while(B!=NULL)
{
B = B->next;
lenB++;
}
A = headA, B = headB;
if(lenA>lenB)
for(int i=0;i<lenA-lenB;i++)
A = A->next;
else
for(int i=0;i<lenB-lenA;i++)
B = B->next;
if(A==B) return A;
while(A!=B && A!=NULL && B!=NULL)
{
A=A->next;
B=B->next;
if(A==B) return A;
}
return NULL;
}
};
这题比较有意思的地方是在头节点前新增了一个节点,这样不需判断边界问题,简化了特判操作。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *C = new ListNode();
C->next = head;
ListNode *A = C;
ListNode *B = C;
for (int i = 1; i <= n + 1; i++) {
A = A->next;
}
while (A != NULL) {
A = A->next;
B = B->next;
}
B->next = B->next->next;
return C->next;
}
};