Skip to content

Latest commit

 

History

History
60 lines (41 loc) · 1.13 KB

_1669. Merge In Between Linked Lists.md

File metadata and controls

60 lines (41 loc) · 1.13 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 24, 2024

Last updated : June 24, 2024


Related Topics : Linked List

Acceptance Rate : 81.99 %


Solutions

C

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


struct ListNode* mergeInBetween(struct ListNode* list1, int a, int b, struct ListNode* list2){

    struct ListNode* aMinusOne = list1;

    for (int i = 0; i < a - 1; i++) {
        aMinusOne = aMinusOne->next;
    }

    struct ListNode* bPlusOne = aMinusOne;

    for (int i = a - 1; i <= b; i++) {
        bPlusOne = bPlusOne->next;
    }

    aMinusOne->next = list2;

    while (aMinusOne->next) {
        aMinusOne = aMinusOne->next;
    }

    aMinusOne->next = bPlusOne;
    return list1;
}