链表中等题(上)
| |
| int gcd(int a, int b) { |
| if (a % b == 0) return b; |
| return gcd(b, a % b); |
| } |
| |
| struct ListNode *insertGreatestCommonDivisors(struct ListNode *head) { |
| if (head->next == NULL) return head; |
| struct ListNode *p = head; |
| while (p->next != NULL) { |
| int value = gcd(p->val, p->next->val); |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->val = value; |
| node->next = p->next; |
| p->next = node; |
| p = p->next->next; |
| } |
| return head; |
| } |
| void deleteNode(struct ListNode *node) { |
| |
| node->val = node->next->val; |
| node->next = node->next->next; |
| } |
| struct ListNode *mergeNodes(struct ListNode *head) { |
| struct ListNode *p = head->next; |
| struct ListNode *pre = head; |
| int tempSum = 0; |
| while (p != NULL) { |
| if (p->val != 0) { |
| tempSum += p->val; |
| } else { |
| |
| pre->val = tempSum; |
| |
| pre->next = p; |
| tempSum = 0; |
| if(p->next == NULL) |
| |
| pre->next = NULL; |
| else |
| pre = p; |
| } |
| p = p->next; |
| } |
| return head; |
| } |
| struct ListNode **listOfDepth(struct TreeNode *tree, int *returnSize) { |
| struct ListNode **res = (struct ListNode **) malloc(sizeof(struct ListNode *) * 100); |
| *returnSize = 0; |
| |
| |
| const int size = 100; |
| struct TreeNode *queue[size]; |
| int front = 0, rear = 0; |
| queue[rear++] = tree; |
| |
| while (front != rear) { |
| |
| int count = (rear - front + size) % size; |
| |
| struct ListNode *tempHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| struct ListNode *pre = tempHead; |
| while (count-- > 0) { |
| |
| struct TreeNode *treeNode = queue[(front++) % size]; |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->val = treeNode->val; |
| node->next = nullptr; |
| pre->next = node; |
| pre = node; |
| |
| if (treeNode->left != nullptr) queue[(rear++) % size] = treeNode->left; |
| if (treeNode->right != nullptr) queue[(rear++) % size] = treeNode->right; |
| } |
| res[(*returnSize)++] = tempHead->next; |
| } |
| |
| return res; |
| } |
| |
| struct ListNode *findMid(struct ListNode *head) { |
| if (head == NULL || head->next == NULL)return head; |
| struct ListNode *slow = head, *fast = head; |
| while (fast != NULL && fast->next != NULL) { |
| slow = slow->next; |
| fast = fast->next->next; |
| } |
| return slow; |
| } |
| |
| |
| struct ListNode *reverseList(struct ListNode *head) { |
| struct ListNode *cur = head; |
| struct ListNode *pre = NULL; |
| struct ListNode *next; |
| while (cur != NULL) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| return pre; |
| } |
| |
| int pairSum(struct ListNode *head) { |
| struct ListNode *mid = findMid(head); |
| struct ListNode *newMid = reverseList(mid); |
| printf("%d %d\n", mid->val, newMid->val); |
| int maxSum = 0; |
| while (newMid != NULL) { |
| if (head->val + newMid->val > maxSum) |
| maxSum = head->val + newMid->val; |
| head = head->next; |
| newMid = newMid->next; |
| } |
| return maxSum; |
| } |
| struct ListNode *mergeInBetween(struct ListNode *list1, int a, int b, struct ListNode *list2) { |
| struct ListNode *slow = list1, *fast = list1; |
| |
| while (--a > 0) { |
| slow = slow->next; |
| } |
| |
| while (b-- >= 0) { |
| fast = fast->next; |
| } |
| |
| slow->next = list2; |
| while (list2->next != NULL) { |
| list2 = list2->next; |
| } |
| list2->next = fast; |
| return list1; |
| } |
| |
| |
| struct TreeNode *generate(int *array, int left, int right) { |
| int mid = ((right - left) >> 1) + left; |
| struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode)); |
| node->val = array[mid]; |
| node->left = generate(array, left, mid - 1); |
| node->right = generate(array, mid + 1, right); |
| return node; |
| } |
| |
| |
| struct TreeNode *sortedListToBST(struct ListNode *head) { |
| if (head == nullptr) return nullptr; |
| int len = 0; |
| struct ListNode *p = head; |
| while (p != nullptr) { |
| len++; |
| p = p->next; |
| } |
| |
| int array[len]; |
| p = head; |
| for (int i = 0; i < len; ++i) { |
| array[i] = p->val; |
| p = p->next; |
| } |
| |
| return generate(array, 0, len - 1); |
| } |
| struct TreeNode *sortedListToBST(struct ListNode *head) { |
| |
| if (head == NULL) return NULL; |
| struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode)); |
| |
| if (head->next == NULL) { |
| node->val = head->val; |
| node->left = NULL; |
| node->right = NULL; |
| return node; |
| } |
| |
| |
| struct ListNode *slow = head, *fast = head, *pre = NULL; |
| while (fast != NULL && fast->next != NULL) { |
| pre = slow; |
| slow = slow->next; |
| fast = fast->next->next; |
| } |
| |
| pre->next = NULL; |
| |
| node->val = slow->val; |
| |
| node->left = sortedListToBST(head); |
| node->right = sortedListToBST(slow->next); |
| return node; |
| } |
| |
| struct ListNode *pre; |
| |
| struct TreeNode *inorder(int left, int right) { |
| if (left > right) return NULL; |
| int mid = left + ((right - left) >> 1); |
| struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode)); |
| |
| |
| struct TreeNode *leftNode = inorder(left, mid - 1); |
| |
| node->val = pre->val; |
| pre = pre->next; |
| |
| struct TreeNode *rightNode = inorder(mid + 1, right); |
| |
| node->left = leftNode; |
| node->right = rightNode; |
| |
| return node; |
| } |
| |
| struct TreeNode *sortedListToBST(struct ListNode *head) { |
| |
| if (head == NULL) return NULL; |
| struct TreeNode *node = (struct TreeNode *) malloc(sizeof(struct TreeNode)); |
| |
| if (head->next == NULL) { |
| node->val = head->val; |
| node->left = NULL; |
| node->right = NULL; |
| return node; |
| } |
| |
| int len = 0; |
| struct ListNode *temp = head; |
| while (temp != NULL) { |
| len++; |
| temp = temp->next; |
| } |
| |
| pre = head; |
| return inorder(0, len - 1); |
| } |
| |
| void flatten(struct TreeNode *root) { |
| if (root == NULL) return; |
| struct TreeNode *stack[2000]; |
| int top = 0; |
| struct TreeNode *temp, *pre = NULL; |
| stack[top++] = root; |
| |
| while (top != 0) { |
| root = stack[--top]; |
| temp = root; |
| |
| if (root->right != NULL) stack[top++] = root->right; |
| if (root->left != NULL) stack[top++] = root->left; |
| |
| temp->left = NULL; |
| if (pre != NULL) pre->right = temp; |
| pre = temp; |
| } |
| } |
| |
| |
| void flatten(struct TreeNode *root) { |
| while (root != NULL) { |
| if (root->left != NULL) { |
| struct TreeNode *rightMost = root->left; |
| while (rightMost->right != NULL) |
| rightMost = rightMost->right; |
| |
| rightMost->right = root->right; |
| |
| |
| root->right = root->left; |
| root->left = NULL; |
| } |
| root = root->right; |
| } |
| } |
| struct TreeNode *pre; |
| |
| |
| void dfs(struct TreeNode *root) { |
| if (root == nullptr) return; |
| dfs(root->right); |
| dfs(root->left); |
| root->left = nullptr; |
| root->right = pre; |
| pre = root; |
| } |
| |
| void flatten(struct TreeNode *root) { |
| pre = nullptr; |
| dfs(root); |
| } |
| |
| void flatten(struct TreeNode *root) { |
| if (root == NULL) return; |
| struct TreeNode *stack[2000]; |
| int top = 0; |
| struct TreeNode *pre = NULL; |
| |
| while (top != 0 || root != nullptr) { |
| while (root != nullptr) { |
| stack[top++] = root; |
| root = root->right; |
| } |
| |
| root = stack[--top]; |
| if (root->left != nullptr && pre != root->left) { |
| |
| stack[top++] = root; |
| root = root->left; |
| } else { |
| |
| root->left = NULL; |
| root->right = pre; |
| pre = root; |
| root = nullptr; |
| } |
| } |
| } |
| struct Node *connect(struct Node *root) { |
| if(root == NULL ) return root; |
| const int size = 2049; |
| struct Node *queue[size]; |
| int front = 0, rear = 0; |
| queue[rear++] = root; |
| struct Node *pre, *cur = root; |
| |
| |
| while (front != rear) { |
| int count = (rear - front + size) % size; |
| pre = NULL; |
| while (count-- > 0) { |
| cur = queue[(front++) % size]; |
| if (pre != NULL) pre->next = cur; |
| pre = cur; |
| if (cur->left != NULL) queue[(rear++) % size] = cur->left; |
| if (cur->right != NULL) queue[(rear++) % size] = cur->right; |
| } |
| pre->next = NULL; |
| } |
| |
| return root; |
| } |
| void dfs(struct Node *root) { |
| if (root == NULL) return; |
| struct Node *left = root->left; |
| struct Node *right = root->right; |
| |
| |
| while (left != NULL) { |
| |
| left->next = right; |
| left = left->right; |
| right = right->left; |
| } |
| dfs(root->left); |
| dfs(root->right); |
| } |
| |
| struct Node *connect(struct Node *root) { |
| dfs(root); |
| return root; |
| } |
| |
| struct Node *connect(struct Node *root) { |
| |
| if (root == NULL) return NULL; |
| |
| struct Node *leftMost = root; |
| root->next = NULL; |
| |
| |
| while (leftMost->left != NULL) { |
| struct Node *cur = leftMost; |
| |
| leftMost = leftMost->left; |
| struct Node *pre = NULL; |
| |
| |
| while (cur != NULL) { |
| |
| cur->left->next = cur->right; |
| |
| if (pre != NULL) pre->right->next = cur->left; |
| pre = cur; |
| cur = cur->next; |
| } |
| pre->right->next = NULL; |
| } |
| return root; |
| } |
| |
| typedef struct { |
| int data[10001]; |
| int count; |
| } Solution; |
| |
| |
| Solution *solutionCreate(struct ListNode *head) { |
| Solution *solution = (Solution *) malloc(sizeof(Solution)); |
| solution->count = 0; |
| while (head != NULL) { |
| solution->data[solution->count++] = head->val; |
| head = head->next; |
| } |
| return solution; |
| } |
| |
| int solutionGetRandom(Solution *obj) { |
| int index = (rand() % obj->count); |
| return obj->data[index]; |
| } |
| |
| void solutionFree(Solution *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| typedef struct { |
| struct ListNode *head; |
| } Solution; |
| |
| |
| Solution *solutionCreate(struct ListNode *head) { |
| Solution *solution = (Solution *) malloc(sizeof(Solution)); |
| solution->head = head; |
| return solution; |
| } |
| |
| |
| int solutionGetRandom(Solution *obj) { |
| struct ListNode *cur = obj->head; |
| int res = 10001; |
| int count = 0; |
| while (cur != NULL) { |
| count++; |
| if (res == 10001) res = cur->val; |
| |
| |
| if (rand() % count == 0) res = cur->val; |
| cur = cur->next; |
| } |
| return res; |
| } |
| |
| void solutionFree(Solution *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| struct ListNode *swapPairs(struct ListNode *head) { |
| if (head == NULL || head->next == NULL) return head; |
| struct ListNode *newHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| newHead->next = head; |
| struct ListNode *pre = newHead, *right, *nextNode; |
| |
| while (pre->next != NULL && pre->next->next != NULL) { |
| |
| right = pre->next->next; |
| |
| nextNode = right->next; |
| right->next = pre->next; |
| pre->next = right; |
| right->next->next = nextNode; |
| |
| pre = pre->next->next; |
| } |
| |
| return newHead->next; |
| } |
| |
| struct ListNode *swapPairs(struct ListNode *head) { |
| if (head == nullptr || head->next == nullptr) return head; |
| struct ListNode *nextNode = head->next->next; |
| struct ListNode *right = head->next; |
| right->next = head; |
| head->next = swapPairs(nextNode); |
| return right; |
| } |
| |
| struct ListNode *removeNodes(struct ListNode *head) { |
| if (head == NULL || head->next == NULL)return head; |
| |
| struct ListNode *nextNode = removeNodes(head->next); |
| |
| if (head->val < nextNode->val) return nextNode; |
| head->next = nextNode; |
| return head; |
| } |
| |
| struct ListNode *removeNodes(struct ListNode *head) { |
| int len = 0; |
| struct ListNode *p = head; |
| while (p != NULL) { |
| len++; |
| p = p->next; |
| } |
| struct ListNode *stack[len]; |
| int top = 0; |
| p = head; |
| |
| for (int i = 0; i < len; ++i) { |
| stack[top++] = p; |
| p = p->next; |
| } |
| |
| |
| struct ListNode *pre = stack[--top]; |
| while (top > 0) { |
| struct ListNode *node = stack[--top]; |
| if (node->val >= pre->val) { |
| node->next = pre; |
| pre = node; |
| } |
| } |
| |
| return pre; |
| } |
| struct ListNode *reverseList(struct ListNode *head) { |
| struct ListNode *cur = head, *next, *pre = NULL; |
| while (cur != NULL) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| return pre; |
| } |
| |
| |
| struct ListNode *removeNodes(struct ListNode *head) { |
| if (head->next == NULL)return head; |
| |
| |
| struct ListNode *newHead = reverseList(head); |
| |
| struct ListNode *cur = newHead->next, *pre = newHead; |
| while (cur != NULL) { |
| printf("%d ", cur->val); |
| if (cur->val >= pre->val) { |
| pre->next = cur; |
| pre = cur; |
| } |
| cur = cur->next; |
| } |
| |
| pre->next = NULL; |
| |
| |
| return reverseList(newHead); |
| } |
| struct ListNode *insertionSortList(struct ListNode *head) { |
| if (head->next == NULL) return head; |
| |
| struct ListNode *dummyHead = (struct ListNode*)malloc(sizeof(struct ListNode)); |
| dummyHead->next = head; |
| |
| struct ListNode *lastSorted = head; |
| |
| while (lastSorted->next != NULL) { |
| |
| struct ListNode *cur = lastSorted->next; |
| if (cur->val >= lastSorted->val) { |
| lastSorted = lastSorted->next; |
| } else { |
| |
| struct ListNode *pre = dummyHead; |
| while (pre->next->val <= cur->val) { |
| pre = pre->next; |
| } |
| lastSorted->next = cur->next; |
| cur->next = pre->next; |
| pre->next = cur; |
| } |
| } |
| return dummyHead->next; |
| } |
| struct Node *connect(struct Node *root) { |
| if(root == NULL ) return root; |
| const int size = 2049; |
| struct Node *queue[size]; |
| int front = 0, rear = 0; |
| queue[rear++] = root; |
| struct Node *pre, *cur = root; |
| |
| |
| while (front != rear) { |
| int count = (rear - front + size) % size; |
| pre = NULL; |
| while (count-- > 0) { |
| cur = queue[(front++) % size]; |
| if (pre != NULL) pre->next = cur; |
| pre = cur; |
| if (cur->left != NULL) queue[(rear++) % size] = cur->left; |
| if (cur->right != NULL) queue[(rear++) % size] = cur->right; |
| } |
| pre->next = NULL; |
| } |
| |
| return root; |
| } |
| |
| |
| struct Node *connect(struct Node *root) { |
| if (root == NULL) return NULL; |
| |
| struct Node *leftMost = root; |
| |
| root->next = NULL; |
| |
| |
| while (leftMost != NULL) { |
| printf("leftMost=%d ", leftMost->val); |
| |
| struct Node *cur = leftMost; |
| leftMost = NULL; |
| |
| struct Node *pre = NULL; |
| |
| while (cur != NULL) { |
| |
| if (leftMost == NULL) { |
| if (cur->left != NULL) |
| leftMost = cur->left; |
| else if (cur->right != NULL) |
| leftMost = cur->right; |
| } |
| |
| |
| if (cur->left != NULL && cur->right != NULL) { |
| cur->left->next = cur->right; |
| if (pre != NULL) pre->next = cur->left; |
| pre = cur->right; |
| } else if (cur->left == NULL && cur->right != NULL) { |
| if (pre != NULL) pre->next = cur->right; |
| pre = cur->right; |
| } else if (cur->left != NULL && cur->right == NULL) { |
| if (pre != NULL) pre->next = cur->left; |
| pre = cur->left; |
| } |
| cur = cur->next; |
| } |
| |
| if (pre != NULL) pre->next = NULL; |
| } |
| return root; |
| } |
| |
| |
| struct Node *connect(struct Node *root) { |
| if (root == NULL) return NULL; |
| struct Node *dummyHead = (struct Node *) malloc(sizeof(struct Node)); |
| |
| struct Node *cur = root; |
| |
| |
| while (cur != NULL) { |
| dummyHead->next = NULL; |
| |
| struct Node *pre = dummyHead; |
| |
| |
| while (cur != NULL) { |
| if (cur->left != NULL) { |
| pre->next = cur->left; |
| pre = pre->next; |
| } |
| if (cur->right != NULL) { |
| pre->next = cur->right; |
| pre = pre->next; |
| } |
| cur = cur->next; |
| } |
| cur = dummyHead->next; |
| } |
| return root; |
| } |
| int **spiralMatrix(int m, int n, struct ListNode *head, int *returnSize, int **returnColumnSizes) { |
| |
| int **res = (int **) malloc(sizeof(int *) * m); |
| *returnColumnSizes = (int *) malloc(sizeof(int) * m); |
| *returnSize = m; |
| for (int i = 0; i < m; ++i) { |
| res[i] = (int *) malloc(sizeof(int) * n); |
| (*returnColumnSizes)[i] = n; |
| } |
| |
| for (int i = 0; i < m; ++i) { |
| for (int j = 0; j < n; ++j) { |
| res[i][j] = -1; |
| } |
| } |
| |
| struct ListNode *cur = head; |
| int i = 0, j = 0; |
| while (cur != NULL) { |
| |
| while (cur != NULL |
| && j < n |
| && res[i][j] == -1) { |
| res[i][j++] = cur->val; |
| printf("%d ", cur->val); |
| cur = cur->next; |
| } |
| j--; |
| i++; |
| |
| |
| while (cur != NULL |
| && i < m |
| && res[i][j] == -1) { |
| res[i++][j] = cur->val; |
| printf("%d ", cur->val); |
| cur = cur->next; |
| } |
| i--; |
| j--; |
| |
| |
| while (cur != NULL |
| && j >= 0 |
| && res[i][j] == -1) { |
| res[i][j--] = cur->val; |
| printf("%d ", cur->val); |
| cur = cur->next; |
| } |
| j++; |
| i--; |
| |
| |
| while (cur != NULL |
| && i >= 0 |
| && res[i][j] == -1) { |
| res[i--][j] = cur->val; |
| printf("%d ", cur->val); |
| cur = cur->next; |
| } |
| i++; |
| j++; |
| } |
| |
| return res; |
| } |
| int **spiralMatrix(int m, int n, struct ListNode *head, int *returnSize, int **returnColumnSizes) { |
| int **res = (int **) malloc(sizeof(int *) * m); |
| *returnColumnSizes = (int *) malloc(sizeof(int) * m); |
| *returnSize = m; |
| for (int i = 0; i < m; ++i) { |
| res[i] = (int *) malloc(sizeof(int) * n); |
| (*returnColumnSizes)[i] = n; |
| } |
| |
| for (int i = 0; i < m; ++i) { |
| for (int j = 0; j < n; ++j) { |
| res[i][j] = -1; |
| } |
| } |
| |
| struct ListNode *cur = head; |
| int i = 0, j = 0; |
| while (cur != NULL) { |
| |
| while (cur != NULL |
| && j < n |
| && res[i][j] == -1) { |
| res[i][j++] = cur->val; |
| printf("%d ", cur->val); |
| cur = cur->next; |
| } |
| j--; |
| i++; |
| |
| |
| while (cur != NULL |
| && i < m |
| && res[i][j] == -1) { |
| res[i++][j] = cur->val; |
| printf("%d ", cur->val); |
| cur = cur->next; |
| } |
| i--; |
| j--; |
| |
| |
| while (cur != NULL |
| && j >= 0 |
| && res[i][j] == -1) { |
| res[i][j--] = cur->val; |
| printf("%d ", cur->val); |
| cur = cur->next; |
| } |
| j++; |
| i--; |
| |
| |
| while (cur != NULL |
| && i >= 0 |
| && res[i][j] == -1) { |
| res[i--][j] = cur->val; |
| printf("%d ", cur->val); |
| cur = cur->next; |
| } |
| i++; |
| j++; |
| } |
| |
| return res; |
| } |
| struct Node *copyRandomList(struct Node *head) { |
| if (head == NULL) return NULL; |
| struct Node *pre = head; |
| |
| |
| while (pre != NULL) { |
| struct Node *node = (struct Node *) malloc(sizeof(struct Node)); |
| node->val = pre->val; |
| |
| node->next = pre->next; |
| pre->next = node; |
| pre = pre->next->next; |
| } |
| |
| pre = head; |
| while (pre != NULL) { |
| |
| if (pre->random != NULL) |
| pre->next->random = pre->random->next; |
| else |
| pre->next->random = NULL; |
| pre = pre->next->next; |
| } |
| |
| pre = head; |
| struct Node *res = head->next, *cur = head->next; |
| while (cur != NULL && cur->next != NULL) { |
| |
| pre->next = pre->next->next; |
| pre = pre->next; |
| |
| cur->next = cur->next->next; |
| cur = cur->next; |
| } |
| |
| pre->next = NULL; |
| return res; |
| } |
| struct ListNode *findMid(struct ListNode *root) { |
| if (root->next == NULL) return NULL; |
| struct ListNode *slow = root, *fast = root; |
| while (fast != NULL && fast->next != NULL) { |
| slow = slow->next; |
| fast = fast->next->next; |
| } |
| return slow; |
| } |
| |
| struct ListNode *reverseList(struct ListNode *root) { |
| struct ListNode *cur = root, *pre = NULL, *next; |
| while (cur != NULL) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| return pre; |
| } |
| |
| void reorderList(struct ListNode *head) { |
| if (head->next == NULL) return; |
| struct ListNode *mid = findMid(head); |
| struct ListNode *l1 = head; |
| struct ListNode *l2 = reverseList(mid); |
| |
| while (l1->next != mid) { |
| struct ListNode *temp = l2->next; |
| l2->next = l1->next; |
| l1->next = l2; |
| l1 = l1->next->next; |
| l2 = temp; |
| } |
| l1->next = l2; |
| } |
| |
| struct ListNode *findMid(struct ListNode *head) { |
| if (head == NULL && head->next == NULL) return head; |
| struct ListNode *slow = head, *fast = head->next; |
| while (fast != NULL && fast->next != NULL) { |
| slow = slow->next; |
| fast = fast->next->next; |
| } |
| return slow; |
| } |
| |
| |
| struct ListNode *merge(struct ListNode *l1, struct ListNode *l2) { |
| if (l1 == NULL || l2 == NULL) return l1 == NULL ? l2 : l1; |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| struct ListNode *pre = dummyHead; |
| while (l1 != NULL && l2 != NULL) { |
| if (l1->val < l2->val) { |
| pre->next = l1; |
| l1 = l1->next; |
| } else { |
| pre->next = l2; |
| l2 = l2->next; |
| } |
| pre = pre->next; |
| } |
| |
| if (l1 != NULL) pre->next = l1; |
| if (l2 != NULL) pre->next = l2; |
| return dummyHead->next; |
| } |
| |
| |
| struct ListNode *divide(struct ListNode *head) { |
| if (head == NULL || head->next == NULL) return head; |
| struct ListNode *mid = findMid(head); |
| struct ListNode *right = mid->next; |
| mid->next = NULL; |
| struct ListNode *l1 = divide(head); |
| struct ListNode *l2 = divide(right); |
| return merge(l1, l2); |
| } |
| |
| struct ListNode *sortList(struct ListNode *head) { |
| return divide(head); |
| } |
| |
| |
| struct ListNode *merge(struct ListNode *l1, struct ListNode *l2) { |
| if (l1 == NULL || l2 == NULL) return l1 == NULL ? l2 : l1; |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| struct ListNode *pre = dummyHead; |
| while (l1 != NULL && l2 != NULL) { |
| if (l1->val < l2->val) { |
| pre->next = l1; |
| l1 = l1->next; |
| } else { |
| pre->next = l2; |
| l2 = l2->next; |
| } |
| pre = pre->next; |
| } |
| |
| if (l1 != NULL) pre->next = l1; |
| if (l2 != NULL) pre->next = l2; |
| return dummyHead->next; |
| } |
| |
| |
| struct ListNode *sortList(struct ListNode *head) { |
| |
| int len = 0; |
| struct ListNode *temp = head; |
| while (temp != NULL) { |
| len++; |
| temp = temp->next; |
| } |
| |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = head; |
| |
| |
| for (int gap = 1; gap < len; gap <<= 1) { |
| struct ListNode *pre = dummyHead; |
| struct ListNode *cur = dummyHead->next; |
| |
| |
| while (cur != NULL) { |
| |
| struct ListNode *l1 = cur; |
| int i = 1; |
| while (i < gap && cur->next != NULL) { |
| cur = cur->next; |
| i++; |
| } |
| |
| |
| struct ListNode *l2 = cur->next; |
| |
| cur->next = NULL; |
| |
| |
| |
| cur = l2; |
| i = 1; |
| while (i < gap && cur != NULL && cur->next != NULL) { |
| cur = cur->next; |
| i++; |
| } |
| |
| |
| struct ListNode *next = NULL; |
| |
| if (cur != NULL) { |
| |
| next = cur->next; |
| |
| cur->next = NULL; |
| } |
| |
| |
| pre->next = merge(l1, l2); |
| |
| while (pre->next != NULL) { |
| pre = pre->next; |
| } |
| |
| cur = next; |
| } |
| } |
| |
| return dummyHead->next; |
| } |
| class Solution { |
| public: |
| Node *res = nullptr; |
| Node *pre = nullptr; |
| |
| void inorder(Node *root) { |
| if (root == nullptr)return; |
| inorder(root->left); |
| if (pre == nullptr) { |
| res = root; |
| } else { |
| pre->right = root; |
| root->left = pre; |
| } |
| pre = root; |
| inorder(root->right); |
| } |
| |
| |
| Node *treeToDoublyList(Node *root) { |
| if (root == nullptr) return nullptr; |
| inorder(root); |
| pre->right = res; |
| res->left = pre; |
| return res; |
| } |
| }; |
| |
| struct ListNode *oddEvenList(struct ListNode *head) { |
| if (head == NULL) return head; |
| struct ListNode *headOfEven = head->next; |
| struct ListNode *odd = head; |
| struct ListNode *even = head->next; |
| while (even != NULL && even->next != NULL) { |
| |
| odd->next = even->next; |
| odd = odd->next; |
| |
| even->next = odd->next; |
| even = even->next; |
| } |
| |
| odd->next = headOfEven; |
| return head; |
| } |
| int *nextLargerNodes(struct ListNode *head, int *returnSize) { |
| int len = 0; |
| |
| struct ListNode *cur = head; |
| while (cur != nullptr) { |
| len++; |
| cur = cur->next; |
| } |
| *returnSize = len; |
| int *res = (int *) calloc(len, sizeof(int)); |
| |
| |
| int list[len]; |
| cur = head; |
| for (int i = 0; i < len; ++i) { |
| list[i] = cur->val; |
| cur = cur->next; |
| } |
| |
| |
| int stack[len]; |
| int top = 0; |
| for (int pos = 0; pos < len; ++pos) { |
| if (top == 0) { |
| |
| stack[top++] = pos; |
| } else { |
| |
| while (top > 0 && list[pos] > list[stack[top - 1]]) { |
| |
| res[stack[top - 1]] = list[pos]; |
| top--; |
| } |
| stack[top++] = pos; |
| } |
| } |
| |
| return res; |
| } |
| |
| |
| struct ListNode *partition(struct ListNode *head, int x) { |
| if (head == NULL || head->next == NULL) return head; |
| struct ListNode *l1 = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| l1->next = NULL; |
| struct ListNode *pre1 = l1; |
| struct ListNode *l2 = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| l2->next = NULL; |
| struct ListNode *pre2 = l2; |
| |
| struct ListNode *cur = head; |
| while (cur != NULL) { |
| if (cur->val < x) { |
| pre1->next = cur; |
| pre1 = pre1->next; |
| } else { |
| pre2->next = cur; |
| pre2 = pre2->next; |
| } |
| cur = cur->next; |
| } |
| pre1->next = l2->next; |
| pre2->next = NULL; |
| return l1->next; |
| } |
| struct ListNode *swapNodes(struct ListNode *head, int k) { |
| struct ListNode *slow = head, *fast = head; |
| struct ListNode *node1, *node2; |
| |
| |
| for (int i = 1; i < k; ++i) |
| fast = fast->next; |
| node1 = fast; |
| |
| while (fast->next != NULL) { |
| slow = slow->next; |
| fast = fast->next; |
| } |
| node2 = slow; |
| |
| |
| int temp = node1->val; |
| node1->val = node2->val; |
| node2->val = temp; |
| return head; |
| } |
| |
| struct MyListNode { |
| char *val; |
| struct MyListNode *next; |
| struct MyListNode *pre; |
| }; |
| |
| typedef struct { |
| struct MyListNode *head; |
| struct MyListNode *cur; |
| } BrowserHistory; |
| |
| |
| BrowserHistory *browserHistoryCreate(char *homepage) { |
| BrowserHistory *obj = (BrowserHistory *) malloc(sizeof(BrowserHistory)); |
| |
| obj->head = (struct MyListNode *) malloc(sizeof(struct MyListNode)); |
| obj->head->pre = NULL; |
| obj->head->val = NULL; |
| |
| struct MyListNode *node = (struct MyListNode *) malloc(sizeof(struct MyListNode)); |
| node->val = homepage; |
| node->next = NULL; |
| node->pre = obj->head; |
| obj->head->next = node; |
| obj->cur = node; |
| return obj; |
| } |
| |
| |
| |
| void browserHistoryVisit(BrowserHistory *obj, char *url) { |
| struct MyListNode *node = (struct MyListNode *) malloc(sizeof(struct MyListNode)); |
| node->val = url; |
| node->next = NULL; |
| node->pre = obj->cur; |
| obj->cur->next = node; |
| obj->cur = node; |
| } |
| |
| char *browserHistoryBack(BrowserHistory *obj, int steps) { |
| while (steps > 0 && obj->cur->pre != obj->head) { |
| steps--; |
| obj->cur = obj->cur->pre; |
| } |
| return obj->cur->val; |
| } |
| |
| char *browserHistoryForward(BrowserHistory *obj, int steps) { |
| while (steps > 0 && obj->cur->next != NULL) { |
| steps--; |
| obj->cur = obj->cur->next; |
| } |
| return obj->cur->val; |
| } |
| |
| void browserHistoryFree(BrowserHistory *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| struct MyListNode { |
| char *val; |
| struct MyListNode *next; |
| }; |
| |
| typedef struct { |
| struct MyListNode *head; |
| struct MyListNode *cur; |
| } BrowserHistory; |
| |
| BrowserHistory *browserHistoryCreate(char *homepage) { |
| BrowserHistory *obj = (BrowserHistory *) malloc(sizeof(BrowserHistory)); |
| |
| obj->head = (struct MyListNode *) malloc(sizeof(struct MyListNode)); |
| obj->head->val = NULL; |
| |
| struct MyListNode *node = (struct MyListNode *) malloc(sizeof(struct MyListNode)); |
| node->val = homepage; |
| node->next = NULL; |
| obj->head->next = node; |
| obj->cur = node; |
| return obj; |
| } |
| |
| |
| void browserHistoryVisit(BrowserHistory *obj, char *url) { |
| struct MyListNode *node = (struct MyListNode *) malloc(sizeof(struct MyListNode)); |
| node->val = url; |
| node->next = NULL; |
| obj->cur->next = node; |
| obj->cur = node; |
| } |
| |
| |
| char *browserHistoryBack(BrowserHistory *obj, int steps) { |
| struct MyListNode *slow = obj->head->next, *fast = obj->head->next; |
| while (steps > 0 && fast != obj->cur) { |
| fast = fast->next; |
| steps--; |
| } |
| while (fast->next != NULL && fast != obj->cur) { |
| slow = slow->next; |
| fast = fast->next; |
| } |
| obj->cur = slow; |
| return slow->val; |
| } |
| |
| char *browserHistoryForward(BrowserHistory *obj, int steps) { |
| while (steps > 0 && obj->cur->next != NULL) { |
| steps--; |
| obj->cur = obj->cur->next; |
| } |
| return obj->cur->val; |
| } |
| |
| void browserHistoryFree(BrowserHistory *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| typedef struct { |
| char **stack; |
| |
| int size; |
| int top; |
| } BrowserHistory; |
| |
| |
| BrowserHistory *browserHistoryCreate(char *homepage) { |
| BrowserHistory *obj = (BrowserHistory *) malloc(sizeof(BrowserHistory)); |
| obj->stack = (char **) malloc(sizeof(char *) * 5001); |
| obj->size = 1; |
| obj->top = 0; |
| obj->stack[0] = homepage; |
| return obj; |
| } |
| |
| void browserHistoryVisit(BrowserHistory *obj, char *url) { |
| obj->stack[++obj->top] = url; |
| obj->size = obj->top + 1; |
| } |
| |
| char *browserHistoryBack(BrowserHistory *obj, int steps) { |
| int index = obj->top - steps; |
| if (index >= 0) { |
| |
| obj->top = index; |
| } else { |
| obj->top = 0; |
| } |
| return obj->stack[obj->top]; |
| } |
| |
| char *browserHistoryForward(BrowserHistory *obj, int steps) { |
| int index = obj->top + steps; |
| |
| if (index < obj->size) |
| obj->top = index; |
| else |
| obj->top = obj->size - 1; |
| return obj->stack[obj->top]; |
| } |
| |
| void browserHistoryFree(BrowserHistory *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| int numComponents(struct ListNode *head, int *nums, int numsSize) { |
| int len = 0; |
| struct ListNode *cur = head; |
| while (cur != NULL) { |
| len++; |
| cur = cur->next; |
| } |
| |
| int *hash = (int *) calloc(len + 1, sizeof(int)); |
| for (int i = 0; i < numsSize; ++i) |
| hash[nums[i]] = 1; |
| |
| int res = 0; |
| bool flag = false; |
| cur = head; |
| while (cur != NULL) { |
| |
| if (hash[cur->val] == 1 && (cur->next == NULL || hash[cur->next->val] == 0)) res++; |
| cur = cur->next; |
| } |
| return res; |
| } |
| struct ListNode *reverseList(struct ListNode *head) { |
| struct ListNode *pre = NULL, *next, *cur = head; |
| while (cur != NULL) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| return pre; |
| } |
| |
| |
| struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) { |
| struct ListNode *n1 = reverseList(l1); |
| struct ListNode *n2 = reverseList(l2); |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = NULL; |
| struct ListNode *pre = dummyHead; |
| |
| |
| int carry = 0; |
| while (n1 != NULL && n2 != NULL) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->next = NULL; |
| |
| node->val = (n1->val + n2->val + carry) % 10; |
| carry = (n1->val + n2->val + carry) / 10; |
| |
| pre->next = node; |
| pre = pre->next; |
| n1 = n1->next; |
| n2 = n2->next; |
| } |
| while (n1 != NULL) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->next = NULL; |
| node->val = (n1->val + carry) % 10; |
| carry = (n1->val + carry) / 10; |
| pre->next = node; |
| pre = pre->next; |
| n1 = n1->next; |
| } |
| while (n2 != NULL) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->next = NULL; |
| node->val = (n2->val + carry) % 10; |
| carry = (n2->val + carry) / 10; |
| pre->next = node; |
| pre = pre->next; |
| n2 = n2->next; |
| } |
| |
| if (carry == 1) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->next = NULL; |
| node->val = 1; |
| pre->next = node; |
| } |
| return reverseList(dummyHead->next); |
| } |
| struct ListNode **splitListToParts(struct ListNode *head, int k, int *returnSize) { |
| struct ListNode **res = (struct ListNode **) malloc(sizeof(struct ListNode *) * k); |
| *returnSize = k; |
| |
| |
| struct ListNode *cur = head; |
| int len = 0; |
| while (cur != NULL) { |
| len++; |
| cur = cur->next; |
| } |
| |
| for (int i = 0; i < k; ++i) res[i] = NULL; |
| |
| |
| int base = len / k; |
| |
| int i = len - base * k; |
| |
| cur = head; |
| int index = 0; |
| while (cur != NULL) { |
| res[index++] = cur; |
| int size = base; |
| |
| if (i-- > 0) size++; |
| while (--size > 0) { |
| cur = cur->next; |
| } |
| |
| struct ListNode *temp = cur->next; |
| |
| cur->next = NULL; |
| cur = temp; |
| } |
| return res; |
| } |
| |
| struct ListNode *partition(struct ListNode *head, int x) { |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = head; |
| struct ListNode *cur = head; |
| struct ListNode *pre = dummyHead; |
| |
| while (cur != NULL) { |
| struct ListNode *next = cur->next; |
| if (cur->val < x) { |
| |
| pre->next = next; |
| |
| cur->next = dummyHead->next; |
| dummyHead->next = cur; |
| |
| if (pre == dummyHead) pre = pre->next; |
| } else { |
| |
| pre = cur; |
| } |
| cur = next; |
| } |
| |
| return dummyHead->next; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步