LeetCode #1669

Merge In Between Linked Lists

1개의 풀이 · C++

문제 원문 보기 ↗

SOLUTION INFO

C++ · main.cpp

main.cpp
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeInBetween(ListNode* list1, int a, int b, ListNode* list2) {
        ListNode *cur = list1;
        int idx = 0;
        while(idx + 1 < a) cur = cur->next, idx++;
        ListNode *cur2 = cur;
        while(idx <= b) cur2 = cur2->next, idx++;
        ListNode *last = list2;
        while(last && last->next) last = last->next;
        cur->next = list2;
        last->next = cur2;
        return list1;
    }
};

SOLUTION DESCRIPTION

풀이 설명

등록된 풀이 설명이 없습니다.