Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Two Sorted Lists #44

Closed
Tracked by #100
fkdl0048 opened this issue Sep 30, 2024 · 1 comment
Closed
Tracked by #100

Merge Two Sorted Lists #44

fkdl0048 opened this issue Sep 30, 2024 · 1 comment
Assignees
Labels
Milestone

Comments

@fkdl0048
Copy link
Owner

fkdl0048 commented Sep 30, 2024

/**
 * 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* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode* dummy = new ListNode(0);
        ListNode* current = dummy;

        while (list1 != nullptr && list2 != nullptr) {
            if (list1->val <= list2->val) {
                current->next = list1;
                list1 = list1->next;
            } else {
                current->next = list2;
                list2 = list2->next;
            }
            current = current->next;
        }

        if (list1 != nullptr) {
            current->next = list1;
        } else if (list2 != nullptr) {
            current->next = list2;
        }

        return dummy->next;
    }
};
  • 다른 코테에서 많이 본 문제, 공간을 적게 사용하는 문제도 있던 걸로 기억함
  • 더미를 두는 이유 등 연결리스트 개념 정리
@fkdl0048 fkdl0048 mentioned this issue Sep 30, 2024
7 tasks
@fkdl0048 fkdl0048 self-assigned this Sep 30, 2024
@fkdl0048 fkdl0048 added this to Todo Sep 30, 2024
@github-project-automation github-project-automation bot moved this to Todo in Todo Sep 30, 2024
@fkdl0048 fkdl0048 moved this from Todo to Two-Week Plan in Todo Sep 30, 2024
@fkdl0048 fkdl0048 added this to the LeetCode milestone Sep 30, 2024
@fkdl0048 fkdl0048 closed this as completed Oct 4, 2024
@github-project-automation github-project-automation bot moved this from Two-Week Plan to Done in Todo Oct 4, 2024
@fkdl0048
Copy link
Owner Author

fkdl0048 commented Oct 4, 2024

복기

  • null말고 nullptr에 대한 이해, ->와, .에 대한 접근
  • 정렬 기준에서 벗어나는 예외처리

@fkdl0048 fkdl0048 mentioned this issue Oct 15, 2024
47 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

No branches or pull requests

1 participant