链表中等题(下)
| class Solution { |
| public: |
| |
| Node *generate(Node *head) { |
| if (head == nullptr) return nullptr; |
| |
| Node *next = generate(head->next); |
| |
| Node *child = generate(head->child); |
| |
| if (child != nullptr) { |
| |
| head->child = nullptr; |
| head->next = child; |
| child->prev = head; |
| |
| if (next != nullptr) { |
| Node *cur = child; |
| |
| while (cur->next != nullptr) { |
| cur = cur->next; |
| } |
| |
| cur->next = next; |
| next->prev = cur; |
| } |
| } |
| return head; |
| } |
| |
| |
| Node *flatten(Node *head) { |
| return generate(head); |
| } |
| }; |
| |
| struct ListNode *detectCycle(struct ListNode *head) { |
| |
| |
| |
| |
| |
| |
| struct ListNode *slow = head, *fast = head; |
| while (fast != NULL && fast->next != NULL) { |
| slow = slow->next; |
| fast = fast->next->next; |
| |
| |
| |
| if (slow == fast) { |
| fast = head; |
| |
| while (slow != fast) { |
| slow = slow->next; |
| fast = fast->next; |
| } |
| |
| return slow; |
| } |
| } |
| |
| return NULL; |
| } |
| |
| struct ListNode *deleteMiddle(struct ListNode *head) { |
| if (head->next == NULL)return NULL; |
| 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 = pre->next->next; |
| return head; |
| } |
| |
| struct ListNode *reverse(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 *doubleIt(struct ListNode *head) { |
| |
| int carry = 0; |
| |
| |
| struct ListNode *newHead = reverse(head); |
| struct ListNode *cur = newHead; |
| while (cur != NULL) { |
| int val = (2 * cur->val + carry) % 10; |
| carry = (2 * cur->val + carry) / 10; |
| cur->val = val; |
| cur = cur->next; |
| } |
| |
| |
| if (carry != 0) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->val = carry; |
| node->next = reverse(newHead); |
| return node; |
| } |
| return reverse(newHead); |
| } |
| |
| struct ListNode *doubleIt(struct ListNode *head) { |
| struct ListNode *res = head; |
| if (head->val > 4) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->val = 1; |
| node->next = head; |
| res = node; |
| } |
| |
| |
| struct ListNode *cur = head; |
| while (cur != NULL) { |
| cur->val = (cur->val * 2) % 10; |
| |
| if (cur->next != NULL && cur->next->val > 4) cur->val++; |
| cur = cur->next; |
| } |
| |
| return res; |
| } |
| |
| int *nodesBetweenCriticalPoints(struct ListNode *head, int *returnSize) { |
| *returnSize = 2; |
| int *res = (int *) calloc(2, sizeof(int)); |
| |
| if (head->next->next == NULL) { |
| res[0] = -1; |
| res[1] = -1; |
| return res; |
| } |
| |
| |
| int preVal = head->val; |
| |
| struct ListNode *cur = head->next; |
| |
| int minIndex = -1; |
| |
| int preIndex = -1; |
| |
| int curIndex = 1; |
| |
| int min = 0x7fffffff; |
| |
| |
| while (cur->next != NULL) { |
| |
| if ((cur->val > preVal && cur->val > cur->next->val) |
| || (cur->val < preVal && cur->val < cur->next->val)) { |
| if (minIndex == -1) |
| minIndex = curIndex; |
| else if (curIndex - preIndex < min) |
| |
| min = curIndex - preIndex; |
| preIndex = curIndex; |
| } |
| |
| preVal = cur->val; |
| curIndex++; |
| cur = cur->next; |
| } |
| |
| |
| if (preIndex == minIndex) { |
| res[0] = -1; |
| res[1] = -1; |
| } else { |
| res[0] = min; |
| |
| res[1] = preIndex - minIndex; |
| } |
| return res; |
| } |
| struct ListNode *reverseBetween(struct ListNode *head, int left, int right) { |
| if (head == NULL || head->next == NULL || left >= right) return head; |
| |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = head; |
| struct ListNode *preNode = dummyHead; |
| int count = left - 1; |
| |
| while (count-- > 0) preNode = preNode->next; |
| |
| struct ListNode *newTail = preNode->next; |
| |
| |
| struct ListNode *pre = NULL, *next = NULL, *cur = preNode->next; |
| count = right - left + 1; |
| while (count-- > 0) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| |
| preNode->next = pre; |
| newTail->next = next; |
| return dummyHead->next; |
| } |
| |
| |
| struct MyListNode { |
| int key; |
| int val; |
| struct MyListNode *prev; |
| struct MyListNode *next; |
| }; |
| |
| |
| typedef struct { |
| struct MyListNode *dummyHead; |
| struct MyListNode *dummyTail; |
| int len; |
| int maxLen; |
| } LRUCache; |
| |
| |
| LRUCache *lRUCacheCreate(int capacity) { |
| LRUCache *cache = (LRUCache *) malloc(sizeof(LRUCache)); |
| cache->dummyHead = (struct MyListNode *) malloc(sizeof(struct MyListNode)); |
| cache->dummyTail = (struct MyListNode *) malloc(sizeof(struct MyListNode)); |
| cache->dummyHead->prev = NULL; |
| cache->dummyHead->next = cache->dummyTail; |
| cache->dummyTail->prev = cache->dummyHead; |
| cache->dummyTail->next = NULL; |
| cache->len = 0; |
| cache->maxLen = capacity; |
| return cache; |
| } |
| |
| |
| void addToHead(struct MyListNode *dummyHead, struct MyListNode *node) { |
| node->next = dummyHead->next; |
| node->prev = dummyHead; |
| node->next->prev = node; |
| dummyHead->next = node; |
| } |
| |
| |
| void moveToHead(struct MyListNode *dummyHead, struct MyListNode *node) { |
| |
| node->prev->next = node->next; |
| node->next->prev = node->prev; |
| |
| addToHead(dummyHead, node); |
| } |
| |
| |
| struct MyListNode *findNode(LRUCache *obj, int key) { |
| struct MyListNode *cur = obj->dummyHead->next; |
| while (cur != obj->dummyTail) { |
| if (cur->key == key) return cur; |
| cur = cur->next; |
| } |
| return NULL; |
| } |
| |
| int lRUCacheGet(LRUCache *obj, int key) { |
| struct MyListNode *node = findNode(obj, key); |
| if (node == NULL) return -1; |
| |
| moveToHead(obj->dummyHead, node); |
| return node->val; |
| } |
| |
| void lRUCachePut(LRUCache *obj, int key, int value) { |
| |
| struct MyListNode *find = findNode(obj, key); |
| if (find != NULL) { |
| find->val = value; |
| moveToHead(obj->dummyHead, find); |
| return; |
| } |
| |
| |
| if (obj->len < obj->maxLen) { |
| struct MyListNode *node = (struct MyListNode *) malloc(sizeof(struct MyListNode)); |
| node->key = key; |
| node->val = value; |
| addToHead(obj->dummyHead, node); |
| |
| if (obj->len == 0) obj->dummyTail->prev = node; |
| obj->len++; |
| } else { |
| |
| |
| struct MyListNode *tail = obj->dummyTail->prev; |
| tail->key = key; |
| tail->val = value; |
| moveToHead(obj->dummyHead, tail); |
| } |
| } |
| |
| void lRUCacheFree(LRUCache *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| |
| class LRUCache { |
| ListNode dummyHead; |
| ListNode dummyTail; |
| int capacity; |
| Map<Integer, ListNode> map; |
| |
| public LRUCache(int capacity) { |
| map = new HashMap<>(); |
| this.capacity = capacity; |
| dummyHead = new ListNode(); |
| dummyTail = new ListNode(); |
| dummyHead.next = dummyTail; |
| dummyTail.prev = dummyHead; |
| } |
| |
| public int get(int key) { |
| if (!map.containsKey(key)) return -1; |
| ListNode node = map.get(key); |
| moveToHead(node); |
| return node.value; |
| } |
| |
| public void addToHead(ListNode node) { |
| map.put(node.key, node); |
| node.next = dummyHead.next; |
| dummyHead.next.prev = node; |
| dummyHead.next = node; |
| node.prev = dummyHead; |
| } |
| |
| public void moveToHead(ListNode node) { |
| |
| node.prev.next = node.next; |
| node.next.prev = node.prev; |
| |
| node.next = dummyHead.next; |
| dummyHead.next.prev = node; |
| node.prev = dummyHead; |
| dummyHead.next = node; |
| } |
| |
| public void deleteNode(ListNode node) { |
| map.remove(node.key); |
| node.prev.next = node.next; |
| node.next.prev = node.prev; |
| } |
| |
| public void put(int key, int value) { |
| if (map.containsKey(key)) { |
| ListNode node = map.get(key); |
| node.value = value; |
| moveToHead(node); |
| return; |
| } |
| |
| if (map.size() == capacity) deleteNode(dummyTail.prev); |
| |
| ListNode node = new ListNode(key, value); |
| addToHead(node); |
| } |
| } |
| |
| class ListNode { |
| int key; |
| int value; |
| ListNode prev; |
| ListNode next; |
| |
| public ListNode(int key, int value, ListNode prev, ListNode next) { |
| this.key = key; |
| this.value = value; |
| this.prev = prev; |
| this.next = next; |
| } |
| |
| public ListNode(int key, int value) { |
| this.key = key; |
| this.value = value; |
| } |
| |
| public ListNode() { |
| } |
| } |
| |
| struct MyNode { |
| int key; |
| int val; |
| }; |
| |
| typedef struct { |
| struct MyNode *queue; |
| int front; |
| int rear; |
| int size; |
| } LRUCache; |
| |
| LRUCache *lRUCacheCreate(int capacity) { |
| LRUCache *cache = (LRUCache *) malloc(sizeof(LRUCache)); |
| |
| cache->size = capacity + 1; |
| cache->front = 0; |
| cache->rear = 0; |
| cache->queue = (struct MyNode *) calloc(capacity + 1, sizeof(struct MyNode)); |
| return cache; |
| } |
| |
| |
| int getLen(LRUCache *obj) { |
| return (obj->rear - obj->front + obj->size) % obj->size; |
| } |
| |
| bool isFull(LRUCache *obj) { |
| return getLen(obj) == obj->size - 1; |
| } |
| |
| void addToHead(LRUCache *obj, int key, int value) { |
| obj->front = (obj->front - 1 + obj->size) % obj->size; |
| obj->queue[obj->front].key = key; |
| obj->queue[obj->front].val = value; |
| } |
| |
| void moveToHead(LRUCache *obj, int index) { |
| obj->front = (obj->front - 1 + obj->size) % obj->size; |
| obj->queue[obj->front].key = obj->queue[index].key; |
| obj->queue[obj->front].val = obj->queue[index].val; |
| while (index != obj->rear) { |
| |
| obj->queue[index].key = obj->queue[(index + 1) % obj->size].key; |
| obj->queue[index].val = obj->queue[(index + 1) % obj->size].val; |
| index = (index + 1 + obj->size) % obj->size; |
| } |
| obj->rear = (obj->rear - 1 + obj->size) % obj->size; |
| } |
| |
| int findNodeIndex(LRUCache *obj, int key) { |
| int count = getLen(obj); |
| int index = obj->front; |
| while (count-- > 0) { |
| if (obj->queue[index].key == key) return index; |
| index = (index + 1 + obj->size) % obj->size; |
| } |
| return -1; |
| } |
| |
| int lRUCacheGet(LRUCache *obj, int key) { |
| int index = findNodeIndex(obj, key); |
| if (index == -1) return -1; |
| int res = obj->queue[index].val; |
| moveToHead(obj, index); |
| return res; |
| } |
| |
| void lRUCachePut(LRUCache *obj, int key, int value) { |
| |
| int index = findNodeIndex(obj, key); |
| if (index != -1) { |
| obj->queue[index].val = value; |
| moveToHead(obj, index); |
| return; |
| } |
| |
| if (isFull(obj)) { |
| obj->rear = (obj->rear - 1 + obj->size) % obj->size; |
| } |
| |
| addToHead(obj, key, value); |
| } |
| |
| void lRUCacheFree(LRUCache *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| |
| struct MyNode { |
| int key; |
| int val; |
| }; |
| |
| typedef struct { |
| struct MyNode *queue; |
| int front; |
| int rear; |
| int size; |
| int *hashMap; |
| } LRUCache; |
| |
| LRUCache *lRUCacheCreate(int capacity) { |
| LRUCache *cache = (LRUCache *) malloc(sizeof(LRUCache)); |
| |
| cache->size = capacity + 1; |
| cache->front = 0; |
| cache->rear = 0; |
| cache->queue = (struct MyNode *) calloc(capacity + 1, sizeof(struct MyNode)); |
| |
| cache->hashMap = (int *) malloc(sizeof(int) * 6000); |
| for (int i = 0; i < 6000; ++i) { |
| cache->hashMap[i] = -1; |
| } |
| return cache; |
| } |
| |
| |
| int getLen(LRUCache *obj) { |
| return (obj->rear - obj->front + obj->size) % obj->size; |
| } |
| |
| bool isFull(LRUCache *obj) { |
| return getLen(obj) == obj->size - 1; |
| } |
| |
| void addToHead(LRUCache *obj, int key, int value) { |
| obj->front = (obj->front - 1 + obj->size) % obj->size; |
| obj->queue[obj->front].key = key; |
| obj->queue[obj->front].val = value; |
| obj->hashMap[key] = obj->front; |
| } |
| |
| void moveToHead(LRUCache *obj, int index) { |
| obj->front = (obj->front - 1 + obj->size) % obj->size; |
| obj->queue[obj->front].key = obj->queue[index].key; |
| obj->queue[obj->front].val = obj->queue[index].val; |
| obj->hashMap[obj->queue[obj->front].key] = obj->front; |
| |
| int count = (obj->rear - index + obj->size) % obj->size - 1; |
| while (count-- > 0) { |
| |
| obj->queue[index].key = obj->queue[(index + 1) % obj->size].key; |
| obj->queue[index].val = obj->queue[(index + 1) % obj->size].val; |
| obj->hashMap[obj->queue[index].key] = index; |
| index = (index + 1 + obj->size) % obj->size; |
| } |
| obj->rear = (obj->rear - 1 + obj->size) % obj->size; |
| |
| |
| if (obj->hashMap[obj->queue[obj->rear].key] == obj->rear) obj->hashMap[obj->queue[obj->rear].key] = -1; |
| } |
| |
| int findNodeIndex(LRUCache *obj, int key) { |
| return obj->hashMap[key]; |
| } |
| |
| int lRUCacheGet(LRUCache *obj, int key) { |
| int index = findNodeIndex(obj, key); |
| if (index == -1) return -1; |
| int res = obj->queue[index].val; |
| moveToHead(obj, index); |
| return res; |
| } |
| |
| void lRUCachePut(LRUCache *obj, int key, int value) { |
| |
| int index = findNodeIndex(obj, key); |
| if (index != -1) { |
| obj->queue[index].val = value; |
| moveToHead(obj, index); |
| return; |
| } |
| |
| if (isFull(obj)) { |
| obj->rear = (obj->rear - 1 + obj->size) % obj->size; |
| if (obj->hashMap[obj->queue[obj->rear].key] == obj->rear) obj->hashMap[obj->queue[obj->rear].key] = -1; |
| } |
| |
| addToHead(obj, key, value); |
| } |
| |
| void lRUCacheFree(LRUCache *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| struct ListNode *removeNthFromEnd(struct ListNode *head, int n) { |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = head; |
| struct ListNode *slow = dummyHead, *fast = dummyHead; |
| |
| int count = n + 1; |
| while (count-- > 0) fast = fast->next; |
| while (fast != NULL) { |
| slow = slow->next; |
| fast = fast->next; |
| } |
| |
| slow->next = slow->next->next; |
| return dummyHead->next; |
| } |
| struct ListNode *deleteDuplicates(struct ListNode *head) { |
| if (head == NULL || head->next == NULL) return head; |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = head; |
| |
| struct ListNode *pre = dummyHead, *cur = head; |
| |
| while (cur != NULL) { |
| |
| bool flag = false; |
| while (cur->next != NULL && cur->next->val == cur->val) { |
| flag = true; |
| cur = cur->next; |
| } |
| if (flag) { |
| |
| pre->next = cur->next; |
| |
| } else { |
| |
| pre = cur; |
| } |
| cur = cur->next; |
| } |
| return dummyHead->next; |
| } |
| |
| struct ListNode *deleteDuplicates(struct ListNode *head) { |
| if (head == NULL || head->next == NULL) return head; |
| struct ListNode *cur = head; |
| |
| bool flag = false; |
| while (cur->next != NULL && cur->next->val == head->val) { |
| flag = true; |
| cur = cur->next; |
| } |
| |
| |
| if (flag) { |
| |
| return deleteDuplicates(cur->next); |
| } else { |
| head->next = deleteDuplicates(head->next); |
| return head; |
| } |
| } |
| class Solution { |
| public ListNode removeZeroSumSublists(ListNode head) { |
| Map<Integer, ListNode> map = new HashMap<>(); |
| ListNode dummyHead = new ListNode(); |
| dummyHead.next = head; |
| map.put(0, dummyHead); |
| ListNode cur = head; |
| int sum = 0; |
| |
| while (cur != null) { |
| sum += cur.val; |
| if (map.containsKey(sum)) { |
| |
| ListNode node = map.get(sum); |
| |
| ListNode deleteNode = node.next; |
| int tempSum = sum; |
| while (deleteNode != cur) { |
| tempSum += deleteNode.val; |
| map.remove(tempSum); |
| deleteNode = deleteNode.next; |
| } |
| |
| node.next = cur.next; |
| } else { |
| map.put(sum, cur); |
| } |
| cur = cur.next; |
| } |
| |
| return dummyHead.next; |
| } |
| } |
| |
| class Solution { |
| |
| public ListNode removeZeroSumSublists(ListNode head) { |
| Map<Integer, ListNode> map = new HashMap<>(); |
| ListNode dummyHead = new ListNode(0); |
| dummyHead.next = head; |
| map.put(0, dummyHead); |
| ListNode cur = head; |
| int sum = 0; |
| |
| |
| while (cur != null) { |
| sum += cur.val; |
| map.put(sum, cur); |
| cur = cur.next; |
| } |
| |
| sum = 0; |
| cur = dummyHead; |
| |
| while (cur != null) { |
| sum += cur.val; |
| |
| |
| |
| cur.next = map.get(sum).next; |
| cur = cur.next; |
| } |
| |
| return dummyHead.next; |
| } |
| } |
| struct ListNode *addTwoNumbers(struct ListNode *l1, struct ListNode *l2) { |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = NULL; |
| struct ListNode *pre = dummyHead; |
| |
| |
| int carry = 0; |
| while (l1 != NULL && l2 != NULL) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->val = (l1->val + l2->val + carry) % 10; |
| node->next = NULL; |
| carry = (l1->val + l2->val + carry) / 10; |
| pre->next = node; |
| pre = pre->next; |
| l1 = l1->next; |
| l2 = l2->next; |
| } |
| |
| while (l1 != NULL) { |
| int val = l1->val + carry; |
| l1->val = val % 10; |
| carry = val / 10; |
| pre->next = l1; |
| pre = pre->next; |
| l1 = l1->next; |
| } |
| |
| while (l2 != NULL) { |
| int val = l2->val + carry; |
| l2->val = val % 10; |
| carry = val / 10; |
| pre->next = l2; |
| pre = pre->next; |
| l2 = l2->next; |
| } |
| |
| if (carry != 0) { |
| struct ListNode *node = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| node->val = carry; |
| node->next = NULL; |
| pre->next = node; |
| } |
| |
| return dummyHead->next; |
| } |
| |
| typedef struct { |
| int *queue; |
| int front; |
| int rear; |
| int size; |
| } MyCircularQueue; |
| |
| MyCircularQueue *myCircularQueueCreate(int k) { |
| MyCircularQueue *myCircularQueue = (MyCircularQueue *) malloc(sizeof(MyCircularQueue)); |
| myCircularQueue->size = k + 1; |
| myCircularQueue->front = 0; |
| myCircularQueue->rear = 0; |
| myCircularQueue->queue = (int *) malloc(sizeof(int) * (k + 1)); |
| return myCircularQueue; |
| } |
| |
| int getLen(MyCircularQueue *obj) { |
| return (obj->rear - obj->front + obj->size) % obj->size; |
| } |
| |
| bool myCircularQueueIsEmpty(MyCircularQueue *obj) { |
| return obj->rear == obj->front; |
| } |
| |
| bool myCircularQueueIsFull(MyCircularQueue *obj) { |
| return getLen(obj) == obj->size - 1; |
| } |
| |
| bool myCircularQueueEnQueue(MyCircularQueue *obj, int value) { |
| if (myCircularQueueIsFull(obj)) return false; |
| obj->queue[obj->rear] = value; |
| obj->rear = (obj->rear + 1 + obj->size) % obj->size; |
| return true; |
| } |
| |
| bool myCircularQueueDeQueue(MyCircularQueue *obj) { |
| if (myCircularQueueIsEmpty(obj)) return false; |
| obj->front = (obj->front + 1 + obj->size) % obj->size; |
| return true; |
| } |
| |
| int myCircularQueueFront(MyCircularQueue *obj) { |
| if (myCircularQueueIsEmpty(obj)) return -1; |
| return obj->queue[obj->front]; |
| } |
| |
| int myCircularQueueRear(MyCircularQueue *obj) { |
| if (myCircularQueueIsEmpty(obj)) return -1; |
| return obj->queue[(obj->rear - 1 + obj->size) % obj->size]; |
| } |
| |
| void myCircularQueueFree(MyCircularQueue *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| |
| typedef struct { |
| int *queue; |
| int front; |
| int rear; |
| int size; |
| } MyCircularDeque; |
| |
| |
| MyCircularDeque *myCircularDequeCreate(int k) { |
| MyCircularDeque *obj = (MyCircularDeque *) malloc(sizeof(MyCircularDeque)); |
| obj->size = k + 1; |
| obj->front = 0; |
| obj->rear = 0; |
| obj->queue = (int *) malloc(sizeof(int) * (k + 1)); |
| return obj; |
| } |
| |
| int getLen(MyCircularDeque *obj) { |
| return (obj->rear - obj->front + obj->size) % obj->size; |
| } |
| |
| bool myCircularDequeIsEmpty(MyCircularDeque *obj) { |
| return obj->rear == obj->front; |
| } |
| |
| bool myCircularDequeIsFull(MyCircularDeque *obj) { |
| return getLen(obj) == obj->size - 1; |
| } |
| |
| bool myCircularDequeInsertFront(MyCircularDeque *obj, int value) { |
| if (myCircularDequeIsFull(obj)) return false; |
| obj->front = (obj->front - 1 + obj->size) % obj->size; |
| obj->queue[obj->front] = value; |
| return true; |
| } |
| |
| bool myCircularDequeInsertLast(MyCircularDeque *obj, int value) { |
| if (myCircularDequeIsFull(obj)) return false; |
| obj->queue[obj->rear] = value; |
| obj->rear = (obj->rear + 1 + obj->size) % obj->size; |
| return true; |
| } |
| |
| bool myCircularDequeDeleteFront(MyCircularDeque *obj) { |
| if (myCircularDequeIsEmpty(obj)) return false; |
| obj->front = (obj->front + 1 + obj->size) % obj->size; |
| return true; |
| } |
| |
| bool myCircularDequeDeleteLast(MyCircularDeque *obj) { |
| if (myCircularDequeIsEmpty(obj)) return false; |
| obj->rear = (obj->rear - 1 + obj->size) % obj->size; |
| return true; |
| } |
| |
| int myCircularDequeGetFront(MyCircularDeque *obj) { |
| if (myCircularDequeIsEmpty(obj)) return -1; |
| return obj->queue[obj->front]; |
| } |
| |
| int myCircularDequeGetRear(MyCircularDeque *obj) { |
| if (myCircularDequeIsEmpty(obj)) return -1; |
| return obj->queue[(obj->rear - 1 + obj->size) % obj->size]; |
| } |
| |
| void myCircularDequeFree(MyCircularDeque *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| |
| struct ListNode *reverseEvenLengthGroups(struct ListNode *head) { |
| struct ListNode *cur = head, *pre = NULL, *tail; |
| for (int i = 1; cur != NULL; ++i) { |
| int count = 0; |
| |
| while (count < i && cur != NULL) { |
| tail = cur; |
| cur = cur->next; |
| count++; |
| } |
| |
| |
| if (count % 2 == 0) { |
| |
| tail = pre->next; |
| |
| |
| struct ListNode *temp = pre->next; |
| struct ListNode *next = NULL; |
| |
| pre->next = cur; |
| |
| |
| while (temp != cur) { |
| |
| next = temp->next; |
| |
| temp->next = pre->next; |
| pre->next = temp; |
| |
| temp = next; |
| } |
| } |
| |
| pre = tail; |
| } |
| return head; |
| } |
| |
| void reverse(struct ListNode *start, struct ListNode *end) { |
| struct ListNode *pre = NULL; |
| struct ListNode *cur = start->next; |
| |
| struct ListNode *last = start->next; |
| while (cur != end) { |
| struct ListNode *next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| |
| start->next = pre; |
| if (last != NULL) last->next = end; |
| } |
| |
| struct ListNode *reverseEvenLengthGroups(struct ListNode *head) { |
| struct ListNode *cur = head; |
| int gap = 2; |
| |
| while (cur != NULL) { |
| |
| struct ListNode *start = cur; |
| |
| struct ListNode *newEnd = cur->next; |
| |
| int count = gap; |
| |
| |
| while (cur != NULL && count > 0) { |
| cur = cur->next; |
| count--; |
| } |
| |
| |
| if (cur == NULL) { |
| |
| count++; |
| |
| if ((gap - count) % 2 == 0) reverse(start, NULL); |
| break; |
| } |
| |
| |
| if (gap % 2 == 0) { |
| reverse(start, cur->next); |
| |
| cur = newEnd; |
| } |
| |
| gap++; |
| } |
| return head; |
| } |
| |
| bool dfsJudge(struct TreeNode *root, struct ListNode *head) { |
| if (head == NULL) return true; |
| if (root == NULL || root->val != head->val) return false; |
| return dfsJudge(root->left, head->next) || dfsJudge(root->right, head->next); |
| } |
| |
| |
| bool dfs(struct TreeNode *root, struct ListNode *head) { |
| if (root == NULL) return false; |
| if (dfsJudge(root, head)) return true; |
| return dfs(root->left, head) || dfs(root->right, head); |
| } |
| |
| bool isSubPath(struct ListNode *head, struct TreeNode *root) { |
| return dfs(root, head); |
| } |
| struct ListNode *reverseBetween(struct ListNode *head, int left, int right) { |
| if (head == NULL || head->next == NULL || left >= right) return head; |
| |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = head; |
| struct ListNode *preNode = dummyHead; |
| int count = left - 1; |
| |
| while (count-- > 0) preNode = preNode->next; |
| |
| struct ListNode *newTail = preNode->next; |
| |
| |
| struct ListNode *pre = NULL, *next = NULL, *cur = preNode->next; |
| count = right - left + 1; |
| while (count-- > 0) { |
| next = cur->next; |
| cur->next = pre; |
| pre = cur; |
| cur = next; |
| } |
| |
| preNode->next = pre; |
| newTail->next = next; |
| return dummyHead->next; |
| } |
| |
| struct ListNode *rotateRight(struct ListNode *head, int k) { |
| if(head == NULL || head->next == NULL) return head; |
| struct ListNode *cur = head; |
| int len = 0; |
| while (cur != NULL) { |
| len++; |
| cur = cur->next; |
| } |
| k %= len; |
| if(k == 0) return head; |
| |
| |
| head = reverseBetween(head, 1, len); |
| head = reverseBetween(head, 1, k); |
| head = reverseBetween(head, k + 1, len); |
| return head; |
| } |
| |
| struct ListNode *rotateRight(struct ListNode *head, int k) { |
| if (head == NULL || head->next == NULL) return head; |
| struct ListNode *cur = head; |
| int len = 0; |
| while (cur != NULL) { |
| len++; |
| cur = cur->next; |
| } |
| k %= len; |
| if (k == 0) return head; |
| |
| struct ListNode *dummyHead = (struct ListNode *) malloc(sizeof(struct ListNode)); |
| dummyHead->next = head; |
| struct ListNode *slow = head; |
| struct ListNode *fast = head; |
| while (k-- > 0) fast = fast->next; |
| while (fast->next != NULL) { |
| slow = slow->next; |
| fast = fast->next; |
| } |
| |
| fast->next = dummyHead->next; |
| dummyHead->next = slow->next; |
| |
| slow->next = NULL; |
| return dummyHead->next; |
| } |
| struct Node { |
| int val; |
| struct Node *pre; |
| struct Node *next; |
| }; |
| |
| |
| typedef struct { |
| struct Node *dummyHead; |
| struct Node *dummyTail; |
| int len; |
| } MyLinkedList; |
| |
| MyLinkedList *myLinkedListCreate() { |
| MyLinkedList *linkedList = (MyLinkedList *) malloc(sizeof(MyLinkedList)); |
| linkedList->dummyHead = (struct Node *) malloc(sizeof(struct Node)); |
| linkedList->dummyTail = (struct Node *) malloc(sizeof(struct Node)); |
| linkedList->dummyHead->pre = NULL; |
| linkedList->dummyHead->next = linkedList->dummyTail; |
| linkedList->dummyTail->pre = linkedList->dummyHead; |
| linkedList->dummyTail->next = NULL; |
| linkedList->len = 0; |
| return linkedList; |
| } |
| |
| |
| struct Node *findNode(MyLinkedList *obj, int index) { |
| if (index >= obj->len) return NULL; |
| if (index < obj->len / 2) { |
| |
| struct Node *cur = obj->dummyHead->next; |
| int count = index; |
| while (count-- > 0) cur = cur->next; |
| return cur; |
| } else { |
| |
| struct Node *cur = obj->dummyTail; |
| int count = obj->len - index; |
| while (count-- > 0) cur = cur->pre; |
| return cur; |
| } |
| } |
| |
| int myLinkedListGet(MyLinkedList *obj, int index) { |
| struct Node *node = findNode(obj, index); |
| if (node == NULL) return -1; |
| return node->val; |
| } |
| |
| void myLinkedListAddAtHead(MyLinkedList *obj, int val) { |
| struct Node *node = (struct Node *) malloc(sizeof(struct Node)); |
| node->val = val; |
| node->next = obj->dummyHead->next; |
| node->pre = obj->dummyHead; |
| obj->dummyHead->next = node; |
| node->next->pre = node; |
| obj->len++; |
| } |
| |
| void myLinkedListAddAtTail(MyLinkedList *obj, int val) { |
| struct Node *node = (struct Node *) malloc(sizeof(struct Node)); |
| node->val = val; |
| node->pre = obj->dummyTail->pre; |
| node->pre->next = node; |
| node->next = obj->dummyTail; |
| obj->dummyTail->pre = node; |
| obj->len++; |
| } |
| |
| void myLinkedListAddAtIndex(MyLinkedList *obj, int index, int val) { |
| if (index > obj->len) return; |
| if (index == obj->len) { |
| myLinkedListAddAtTail(obj, val); |
| return; |
| } |
| |
| struct Node *node = (struct Node *) malloc(sizeof(struct Node)); |
| node->val = val; |
| struct Node *nextNode = findNode(obj, index); |
| node->next = nextNode; |
| node->pre = nextNode->pre; |
| nextNode->pre->next = node; |
| nextNode->pre = node; |
| obj->len++; |
| } |
| |
| void myLinkedListDeleteAtIndex(MyLinkedList *obj, int index) { |
| if (index >= obj->len) return; |
| struct Node *deleteNode = findNode(obj, index); |
| deleteNode->pre->next = deleteNode->next; |
| deleteNode->next->pre = deleteNode->pre; |
| free(deleteNode); |
| deleteNode = NULL; |
| obj->len--; |
| } |
| |
| void myLinkedListFree(MyLinkedList *obj) { |
| free(obj); |
| obj = NULL; |
| } |
| struct Node *findTail(struct Node *head) { |
| struct Node *cur = head; |
| int count = 0; |
| |
| while (count < 2) { |
| if (cur == head) count++; |
| if (cur->val > cur->next->val) return cur; |
| cur = cur->next; |
| } |
| |
| |
| return head; |
| } |
| |
| struct Node *insert(struct Node *head, int insertVal) { |
| struct Node *node = (struct Node *) malloc(sizeof(struct Node)); |
| node->val = insertVal; |
| |
| if (head == NULL) { |
| node->next = node; |
| return node; |
| } |
| |
| if (head->next == head) { |
| node->next = head; |
| head->next = node; |
| return head; |
| } |
| |
| struct Node *tail = findTail(head); |
| struct Node *realHead = tail->next; |
| |
| if (insertVal >= tail->val || (insertVal <= realHead->val)) { |
| node->next = realHead; |
| tail->next = node; |
| return head; |
| } |
| |
| struct Node *cur = realHead; |
| int count = 0; |
| while (count < 2) { |
| |
| if (cur == realHead) count++; |
| |
| if (cur->val <= insertVal && cur->next->val >= insertVal) { |
| node->next = cur->next; |
| cur->next = node; |
| break; |
| } |
| |
| cur = cur->next; |
| } |
| return head; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步