文章目录
初始节点结构:
struct ListNode {
int val;
struct ListNode *next;
}
建链:
struct ListNode* createlist() {
struct ListNode* Hnode = (struct ListNode*)malloc(sizeof(struct ListNode));
Hnode -> next = NULL;
return Hnode;
}
插入:
尾插法:
struct ListNode* insertNode(struct ListNode* list, int num); {
int i;
struct ListNode* tail = list;
for(i = 0; i < n; i++) {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
scanf("%d", &node -> val);
tail -> next = node;
tail = tail -> next;
}
return list;
}
头插法:
struct ListNode* insertNode(struct ListNode* list, int num); {
int i;
struct ListNode* tail = list;
for(i = 0; i < n; i++) {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
scanf("%d", &node -> val);
node -> next = list -> next;
list -> next = node;
}
return list;
}
打印链表:
void printList(struct ListNode* list) {
struct ListNode* p = list -> next;
while(p) {
printf("%d ",p -> val);
p = p->next;
}
}
删除节点:
struct ListNode* deleteList(struct ListNode* head, int val) {
struct ListNode* prev = (struct ListNode*)malloc(sizeof(struct ListNode));
prev -> next = head;
struct ListNode* p = prev;
while(prev != NULL && prev -> next != NULL) {
if(prev -> next -> val == val) {
prev -> next = prev -> next -> next;
} else {
prev = prev -> next;
}
}
return p -> next;
}
反转链表:
迭代法:
struct ListNode* reverseList(struct ListNode* head){
struct ListNode* prev=NULL;
struct ListNode* curr=head;
while(curr){
struct ListNode* next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
}
return prev;
}
递归法:
struct ListNode* reverseList(struct ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
struct ListNode* newHead = reverseList(head -> next);
head -> next -> next = head;
head -> next = NULL;
return newHead;
}
一个完整代码(含输入输出):
#include<stdio.h>
#include<stdlib.h>
struct ListNode
{
int val;
struct ListNode* next;
};
struct ListNode* createlist()
{
struct ListNode* Hnode = (struct ListNode*)malloc(sizeof(struct ListNode));
Hnode -> next = NULL;
return Hnode;
}
struct ListNode* insertNode(struct ListNode* list, int num) {
int i;
struct ListNode* tail = list;
for(i = 0; i < num; i++) {
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode));
scanf("%d", &node -> val);
node -> next = list -> next;
list -> next = node;
}
return list;
}
void printlist(struct ListNode* list) {
struct ListNode* p = list -> next;
while(p) {
printf("%d ",p -> val);
p = p->next;
}
}
struct ListNode* deleteList(struct ListNode* head, int val) {
struct ListNode* prev = (struct ListNode*)malloc(sizeof(struct ListNode));
prev -> next = head;
struct ListNode* p = prev;
while(prev != NULL && prev -> next != NULL) {
if(prev -> next -> val == val) {
prev -> next = prev -> next -> next;
} else {
prev = prev -> next;
}
}
return p -> next;
}
int main(void)
{
struct ListNode* list,* list1;
int num;
scanf("%d", &num);
list = createlist();
list1 = insertNode(list, num);
printlist(list1);
deleteList(list1, 3);
printf("\n");
printlist(list1);
return 0;
}
输入:
5
1 3 5 7 9
(deleteList函数,删除val:3)
输出: