本文共 2560 字,大约阅读时间需要 8 分钟。
????? head ?????????????????????????????????????????????????????????????
??????????????????????????????????????????????????????????????????????
struct ListNode { int val; struct ListNode* next;};struct ListNode* middleNode(struct ListNode* head) { struct ListNode* fast = head, *slow = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } return slow;} ???????????????k????????????????1????
??????????????????k??????????????????????????????????k????
struct ListNode { int val; struct ListNode* next;};struct ListNode* getKthFromEnd(struct ListNode* head, int k) { struct ListNode* fast = head, *slow = head; while (k--) { if (fast == NULL) { return NULL; } fast = fast->next; } while (fast) { fast = fast->next; slow = slow->next; } return slow;} ??????????????????????????????
???????????????????????????????????????????????????????????????????????????????
struct ListNode { int val; struct ListNode* next;};struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { struct ListNode* head = NULL, *tail = NULL; if (l1 == NULL) { return l2; } if (l2 == NULL) { return l1; } if (l1->val < l2->val) { head = tail = l1; l1 = l1->next; } else { head = tail = l2; l2 = l2->next; } while (l1 && l2) { if (l1->val < l2->val) { tail->next = l1; l1 = l1->next; } else { tail->next = l2; l2 = l2->next; } tail = tail->next; } if (l1) { tail->next = l1; } if (l2) { tail->next = l2; } return head;} struct ListNode { int val; struct ListNode* next;};struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { struct ListNode* head = NULL, *tail = NULL; head = tail = (struct ListNode*)malloc(sizeof(struct ListNode)); tail->next = NULL; while (l1 && l2) { if (l1->val < l2->val) { tail->next = l1; l1 = l1->next; } else { tail->next = l2; l2 = l2->next; } tail = tail->next; } if (l1) { tail->next = l1; } if (l2) { tail->next = l2; } struct ListNode* node = head; head = head->next; free(node); return head;} ??????????????????????????????????????????????????????????????
转载地址:http://gxrk.baihongyu.com/