链表相关的操作
typedef struct node { struct node* next; int data; }Element;
一、创建一个单向链表
Element* createSingleLinkedList(int* arr,int size) { Element *head = NULL, *pre = NULL,*p = NULL; for (int i = 0; i < size; i++) { p = (Element*)malloc(sizeof(Element)); if (p == NULL) { return NULL; } else { p->data = arr[i]; p->next = NULL; if (head == NULL) { head = pre = p; } else { pre = pre->next = p; } } } return head; }
递归创建
Element* create(int* arr,int n) { if (n == 0) { return NULL; } Element* head = (Element*)malloc(sizeof(Element)); if (head != NULL) { head->data = arr[n - 1]; head->next = create(arr, n - 1); } return head; }
二、创建单向无头链表
Element* createSingleLinkedWithoutHead(int *arr,int size) { Element *head = NULL,*pre = NULL,*p = NULL; p = (Element*)malloc(sizeof(Element)); if (p == NULL) { return NULL; } p->data = -1; p->next = NULL; head = pre = p; for (int i = 0; i < size; i++) { p = (Element*)malloc(sizeof(Element)); if (p == NULL) { return NULL; } p->data = arr[i]; p->next = NULL; pre = pre->next = p; } return head; }
三、删除链表中某些元素
普通删除
Element* removeVal(Element* head, int val) { for (Element* p = head, *pre = head; p != NULL;) { if (p->data == val) { if (p == head) { head = head->next; delete(p); p = pre = head; } else { pre->next = p->next; delete(p); p = pre->next; } }else{ pre = p; p = p->next; } } return head; }
递归删除
Element* removeElements(Element *head,int val) { if (head == NULL) { return head; } head->next = removeElements(head->next,val); return head->data == val ? head->next : head; }
四、删除链表中指定的节点
void deleteNode(Element *node) { *node = *(node->next); }
思路:需要考虑当前节点存储的不只是数据,下个节点的地址,还有当前节点的地址。将下个节点的数据与下个节点存储的地址赋值给当前节点
http://chuantu.xyz/t6/741/1605495288x1031866013.jpg
五、反转链表
Element* reverse(Element* head) { if (!head || !head->next) return NULL; Element* p = head->next; Element* pre = head; while (head->next != NULL) { head->next = p->next; p->next = pre; pre = p; p = head->next; } return pre; }
递归实现
struct ListNode* reverseList(struct ListNode* head){ if(head==NULL || head->next==NULL){ return head; } struct ListNode * p = reverseList(head->next); head->next->next = head; head->next = NULL; return p; }
非递归节简版
struct ListNode* reverseList(struct ListNode* head){ struct ListNode *p, *pre = NULL; while(head) { p = head->next; head->next = pre; pre = head; head = p; } return pre; }
六、合并2个有序列表
Element* combineLinkedList(Element *h1,Element *h2) { if (h1 == NULL) { return h2; } if (h2 == NULL) { return h1; } Element* head = NULL, * pre = NULL; while (h1 != NULL || h2 != NULL) { if (h1 != NULL && h2 != NULL) { if (h1->data <= h2->data) { if (head == NULL) { pre = head = h1; } else { pre = pre->next = h1; } h1 = h1->next; } else { if (head == NULL) { pre = head = h2; }else{ pre = pre->next = h2; } h2 = h2->next; } } else if (h1 == NULL) { pre->next = h2; h2 = NULL; } else { pre->next = h1; h1 = NULL; } } return head; }
递归版
Element* combineLinkedList_2(Element* h1, Element* h2) { if (h1 == NULL) { return h2; } if (h2 == NULL) { return h1; } if (h1->data > h2->data) { h2->next = combineLinkedList_2(h1,h2->next); return h2; } else { h1->next = combineLinkedList_2(h1->next,h2); return h1; } }