1.含义:将某个变量(对象)赋值给指针(引用),实际上就是就是将这个变量(对象)的地址赋值给指针(引用)。
2.示例:
p—>next = q; 表示p节点的后继指针存储了q节点的内存地址。
p—>next = p—>next—>next; 表示p节点的后继指针存储了p节点的下下个节点的内存地址。
在节点a和节点b之间插入节点x,b是a的下一节点,,p指针指向节点a,则造成指针丢失和内存泄漏的代码:p—>next = x;x—>next = p—>next; 显然这会导致x节点的后继指针指向自身。
正确的写法是2句代码交换顺序,即:x—>next = p—>next; p—>next = x;
在节点a和节点b之间删除节点b,b是a的下一节点,p指针指向节点a:p—>next = p—>next—>next;
1.什么是“哨兵”?
链表中的“哨兵”节点是解决边界问题的,不参与业务逻辑。如果我们引入“哨兵”节点,则不管链表是否为空,head指针都会指向这个“哨兵”节点。我们把这种有“哨兵”节点的链表称为带头链表,或者头结点。相反,没有“哨兵”节点的链表就称为不带头链表或不带头结点的链表。
2.未引入“哨兵”的情况
- //如果在p节点后插入一个节点,只需2行代码即可搞定:
-
- new_node—>next = p—>next;
- p—>next = new_node;
-
- //但,若向空链表中插入一个节点,则代码如下:
-
- if(head == null){
- head = new_node;
- }
-
- //如果要删除节点p的后继节点,只需1行代码即可搞定:
-
- p—>next = p—>next—>next;
-
- //但,若是删除链表的最有一个节点(链表中只剩下这个节点),则代码如下:
-
- if(head—>next == null){
- head = null;
- }
-
从上面的情况可以看出,针对链表的插入、删除操作,需要对插入第一个节点和删除最后一个节点的情况进行特殊处理。这样代码就会显得很繁琐,所以引入“哨兵”节点来解决这个问题。
3.引入“哨兵”的情况
“哨兵”节点不存储数据,无论链表是否为空,head指针都会指向它,作为链表的头结点始终存在。这样,插入第一个节点和插入其他节点,删除最后一个节点和删除其他节点都可以统一为相同的代码实现逻辑了。很方便!
经常用来检查链表是否正确的边界4个边界条件:
1.如果链表为空时,代码是否能正常工作?
2.如果链表只包含一个节点时,代码是否能正常工作?
3.如果链表只包含两个节点时,代码是否能正常工作?
4.代码逻辑在处理头尾节点时是否能正常工作?
数形结合!
拿出我大一上学期写的陈年代码举例😊
常见的链表操作:
链表遍历
- #include
- #include
- struct node
- {
- int num;
- struct node * next;
- };
-
- struct node * creat(int n)
- {
- struct node * head,*q,*p;
- head=(struct node *)malloc(sizeof(struct node));
- head->next=NULL;
- p=head;
- char s[1000];
- int i=0;
- scanf("%s",s);
- while(n--)
- {
- q=(struct node *)malloc(sizeof(struct node));
- q->num=s[i++]-'0';
- q->next=NULL;
- p->next=q;
- p=q;
- }
- return head;
- }
- int main()
- {
- int t,n;
- scanf("%d",&t);
- while(t--)
- {
- int sum=0;
- struct node *list1,*list2,*p,*q;
- scanf("%d",&n);
- list1=creat(n);
- list2=creat(n);
- p=list1->next;
- q=list2->next;
- while(n--)
- {
- //printf("%d %d\n",p->num,q->num);
- if(p->num == q->num)
- sum++;
- p=p->next;
- q=q->next;
- }
- printf("%d\n",sum);
- }
- return 0;
- }
- #include
- #include
- struct node
- {
- int num;
- struct node * next;
- };
-
- struct node * creat(int n)
- {
- int data;
- struct node * head,*p,*q;
- head = (struct node*)malloc(sizeof(struct node));
- head -> next=NULL;
- p=head;
- while(n--)
- {
- scanf("%d",&data);
- q = (struct node*)malloc(sizeof(struct node));
- q -> num = data;
- q -> next = NULL;
- p -> next = q;
- p=q;
- }
- return head;
- }
-
- void in(struct node * head,int data)
- {
- struct node * newNode =(struct node *)malloc(sizeof(struct node));
- newNode->num =data;
- newNode->next=NULL;
- struct node * Move=head->next;
- if(data <= Move->next->num)
- {
- newNode->next=Move->next;
- Move->next=newNode;
- }
- else
- {
- while(Move != NULL)
- {
- if(data>Move->num&&data
next->num) - {
- newNode->next=Move->next;
- Move->next=newNode;
- break;
- }
- Move=Move->next;
- if(Move->next==NULL&&data>Move->num)
- {
- Move->next=newNode;
- break;
- }
- }
- }
- }
- void print(struct node * head)
- {
- struct node * pMove;
- pMove=(struct node *)malloc(sizeof(struct node));
- pMove=head->next->next;
- while(pMove!=NULL)
- {
- printf("%d ",pMove->num);
- pMove=pMove->next;
- }
- printf("\n");
- }
- int main()
- {
- int n,dataa;
- struct node * list;
- scanf("%d",&n);
- list=creat(n);
- dataa=list->next->num;
- //printf("%d",data);
- in(list,dataa);
- print(list);
- return 0;
- }
链表查找
- #include
- #include
- struct node
- {
- int num;
- struct node * next;
- };
-
- struct node * creat(int n)
- {
- int data;
- struct node * head,*p,*q;
- head = (struct node*)malloc(sizeof(struct node));
- head -> next=NULL;
- p=head;
- while(n--)
- {
- scanf("%d",&data);
- q = (struct node*)malloc(sizeof(struct node));
- q -> num = data;
- q -> next = NULL;
- p -> next = q;
- p=q;
- }
- return head;
- }
- void fun(struct node * head)
- struct node * Move;
- Move=(struct node*)malloc(sizeof(struct node));
- Move=head->next;
- int min=Move->num;
- while(Move!=NULL)
- {
- if(min>Move->num)
- {
- min=Move->num;
- }
- Move=Move->next;
- }
- Move=head->next;
- if(min == Move->num)
- return 0;
- else
- {
- struct node * p;
- p=(struct node*)malloc(sizeof(struct node));
- while(Move!=NULL)
- {
- if(Move->next->num == min)
- {
- p=Move->next;
- Move->next=Move->next->next;
- p->next=head->next;
- head->next=p;
- break;
- }
- Move=Move->next;
- }
- }
- }
- void print(struct node * head)
- {
- struct node * pMove;
- pMove=(struct node *)malloc(sizeof(struct node));
- pMove=head->next;
- while(pMove!=NULL)
- {
- printf("%d ",pMove->num);
- pMove=pMove->next;
- }
- printf("\n");
- }
- int main()
- {
- int n,dataa;
- struct node * list;
- scanf("%d",&n);
- list=creat(n);
- fun(list);
- print(list);
- return 0;
- }
链表排列
- #include
- #include
- struct node
- {
- int num;
- struct node * next;
- };
-
- struct node * creat(int n)
- {
- struct node * head,*p,*q;
- head=(struct node *)malloc(sizeof(struct node));
- head->next=NULL;
- p=head;
- while(n--)
- {
- q=(struct node *)malloc(sizeof(struct node));
- scanf("%d",&q->num);
- q->next=NULL;
- p->next=q;
- p=q;
- }
- return head;
- }
- void fun(struct node * head)
- {
- int temp;
- struct node *i,*j;
- for(i=head->next;i!=NULL;i=i->next)
- {
- for(j=i->next;j!=NULL;j=j->next)
- {
- if(j->num < i->num)
- {
- temp = j->num;
- j->num = i->num;
- i->num = temp;
- }
- }
- }
- }
- void print(struct node * head)
- {
- struct node * pMove;
- pMove=(struct node *)malloc(sizeof(struct node));
- pMove=head->next;
- while(pMove!=NULL)
- {
- printf("%d ",pMove->num);
- pMove=pMove->next;
- }
- printf("\n");
- }
- int main()
- {
- struct node * list;
- int n;
- scanf("%d",&n);
- getchar();
- list=creat(n);
- fun(list);
- print(list);
- return 0;
- }
链表去重排序
- #include
- #include
- struct node
- {
- int num;
- struct node * next;
- };
- struct node *creat(int n)
- {
- struct node * head,*p,*q;
- head=(struct node *)malloc(sizeof(struct node));
- head->next=NULL;
- p=head;
- while(n--)
- {
- q=(struct node *)malloc(sizeof(struct node));
- scanf("%d",&q->num);
- q->next=NULL;
- p->next=q;
- p=q;
- }
- return head;
- }
-
- void print(struct node * head)
- {
- struct node * pMove;
- pMove=(struct node *)malloc(sizeof(struct node));
- pMove=head->next;
- while(pMove!=NULL)
- {
- printf("%d ",pMove->num);
- pMove=pMove->next;
- }
- printf("\n");
- }
-
- void delet(struct node * head,int data)
- {
- struct node * posNode=head->next;
- struct node * posNodeFront=head;
- while(posNode!=NULL)
- {
- if(posNode -> num == data)
- {
- posNodeFront->next =posNode->next;
- free(posNode);
- posNode=posNodeFront->next;
-
- }
- else
- {
- posNodeFront=posNode;
- posNode=posNode->next;
- }
-
- }
- }
-
- void fun(struct node * head1,struct node * head2)
- {
- int sign[1001]= {0},si[1001]= {0};
- struct node *Move1,*Move2;
- Move1=head1->next;
- Move2=head2->next;
- while(Move1!=NULL)
- {
- sign[Move1->num]++;
- Move1=Move1->next;
- }
- while(Move2!=NULL)
- {
- si[Move2->num]++;
- Move2=Move2->next;
- }
- /*
- Move1=head1;
- Move2=head2;
- */
- for(int i=0; i<1001; i++)
- {
- if(sign[i]>=1&&si[i]>=1)
- {
- for(int j=0; j
- {
- delet(head1,i);
- delet(head2,i);
- }
- }
- }
- //print(head1);print(head2);
- }
-
- void fun2(struct node * head)
- {
- int temp;
- struct node *i,*j;
- for(i=head->next; i!=NULL; i=i->next)
- {
- for(j=i->next; j!=NULL; j=j->next)
- {
- if(j->num < i->num)
- {
- temp = j->num;
- j->num = i->num;
- i->num = temp;
- }
- }
- }
- }
-
- void fun3(struct node * head)
- {
- int temp;
- struct node *i,*j;
- for(i=head->next; i!=NULL; i=i->next)
- {
- for(j=i->next; j!=NULL; j=j->next)
- {
- if(j->num > i->num)
- {
- temp = j->num;
- j->num = i->num;
- i->num = temp;
- }
- }
- }
- }
-
- int main()
- {
- int m,n;
- while(~scanf("%d%d",&m,&n))
- {
- struct node *list1,*list2;
- list1=creat(m);
- list2=creat(n);
- //print(list1);print(list2);
- fun2(list1);
- fun3(list2);
- //print(list1);print(list2);
- fun(list1,list2);
- print(list1);
- print(list2);
- }
- return 0;
- }
-
单链表反转
- 思路就是两个两个的循环,设置当前位置,父亲位置,和儿子位置,并随着循环改变位置,两个两个的改变箭头方向,画个图就能很形象的理解解题步骤。
- /**
- * 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) {
- struct ListNode *p,*q;
- p=head;
- q=nullptr;
- while(p){
- ListNode *t=p->next;
- p->next=q;
- q=p;
- p=t;
- }
- return q;
- }
- };
链表中环的检测
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode(int x) : val(x), next(NULL) {}
- * };
- */
- class Solution {
- public:
- map
int> m; - ListNode *detectCycle(ListNode *head) {
- while(head){
- if(m[head]!=NULL) return head;//如果该点被记录过,则该点为入口点
- m[head]=1;
- head=head->next;
- }
- return NULL;
- }
- };
- ```
- 利用map标记遍历过的结点即可。在遍历中先进行判断。
两个有序链表合并
- * 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* mergeTwoLists(ListNode* l1, ListNode* l2) {
- ListNode Head(0);//创建新空链表
- ListNode *p= &Head;//让p代表head
- while(l1 && l2){//当l1和l2都没到结尾时
- if(l1->val > l2->val) swap(l1,l2);
- p->next=l1;
- l1=l1->next;
- p=p->next;
- }
- p->next =l1?l1:l2;
- return Head.next;
- }
- };
删除链表倒数第n个节点
- 删除链表的倒数第N个结点,即链表的第len-n个结点。
- 我们设置快慢指针。让快指针先走n步,再让快慢指针一起走。
- tip:为了防止空指针问题,我们应该设置一个头结点,它的next为head。
- 创建: ListNode anspre=new ListNode(-1);
- 然后令快慢指针等于该带有头结点的链表。
- fast=anspre;
- slow=anspre;
- 初始时:
- 🎈快指针所在结点处:n-1
- 🎈慢指针所在结点处:0-1
- 直到快指针为空
- ✨len-1
- ✨len-n-1(此时,慢指针位置是倒数第n个结点的前一个结点,可以通过slow-》next=slow-》next-》next达到删除结点的目的)
-
- ```
- class Solution {
- public:
- ListNode* removeNthFromEnd(ListNode* head, int n) {
- ListNode *fast,*slow;
- ListNode *anspre=new ListNode(-1);
- anspre->next=head;
- if(head==NULL)return {};
- fast=anspre;
- slow=anspre;
- for(int i=0;i<=n;i++){
- fast=fast->next;
- }
- while(fast!=NULL){
- fast=fast->next;
- slow=slow->next;
- }
- slow->next=slow->next->next;
- return anspre->next;
- }
- };
求链表的中间节点
快慢指针,一个一次走两步,一个一次走一步,快指针到达末尾时,慢指针在中间结点处。
合并链表并排序
- #include
- #include
- struct node
- {
- int num;
- struct node * next;
- };
-
- struct node * creat(int n)
- {
- struct node * head,*p,*q;
- head=(struct node *)malloc(sizeof(struct node));
- head->next=NULL;
- p=head;
- while(n--)
- {
- q=(struct node *)malloc(sizeof(struct node));
- scanf("%d",&q->num);
- q->next=NULL;
- p->next=q;
- p=q;
- }
- return head;
- }
- void print(struct node * head)
- {
- struct node * pMove;
- pMove=(struct node *)malloc(sizeof(struct node));
- pMove=head->next;
- while(pMove!=NULL)
- {
- printf("%d ",pMove->num);
- pMove=pMove->next;
- }
- printf("\n");
- }
-
- struct node * fun(struct node * head1,struct node * head2)
- {
- struct node * Move;
- Move=head1->next;
- while(Move->next!=NULL)
- {
- Move=Move->next;
- }
- Move->next=head2->next;
- Move=head1;
- return Move;
- }
- void funnn(struct node * head)
- {
- int temp;
- struct node *i,*j;
- for(i=head->next;i!=NULL;i=i->next)
- {
- for(j=i->next;j!=NULL;j=j->next)
- {
- if(j->num > i->num)
- {
- temp = j->num;
- j->num = i->num;
- i->num = temp;
- }
- }
- }
- }
- int main()
- {
- int m,n;
- struct node *mlist,*nlist,*list;
- scanf("%d",&m);
- mlist=creat(m);
- scanf("%d",&n);
- nlist=creat(n);
- list=fun(mlist,nlist);
- funnn(list);
- print(list);
- return 0;
- }