-
์ฝ๊ฒ ๋งํ๋ฉด ์ซ์๋ฅผ ๋ค์ง์ด์ 1์์๋ฆฌ๋ถํฐ ๋ง์ถฐ์ ๋ฃ์ด์ฃผ๋ ๋ง์ ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋๋ค.
์ด๋ฒ์๋ ์ ๋ง ๋ฌด์ง์ฑ์ผ๋ก ์ฝ๋๋ฅผ ์ง์ ์กฐ๊ฑด๋ฌธ์ด ๋๋ฝ๊ฒ ๋ค์ด๊ฐ๋๋ฐ, ์ข ๋ ๊น๋ํ๊ฒ ํด์ฃผ๋ ค๋ฉด ์ ์กฐ๊ฑด๋ค์ while๋ฌธ์ ์กฐ๊ฑด์ ๊ฑธ์ด์ฃผ๋ฉด ๋๋ค.
/** * 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* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* result = NULL; ListNode* result_pointer = NULL; int up = 0; while(true){ ListNode* temp = new ListNode(); temp -> next = NULL; int val = up; up = 0; if (l1 != NULL) val += l1->val; if (l2 != NULL) val += l2->val; if(val >= 10){ val -= 10; up = 1; } temp -> val = val; if (result_pointer == NULL) { result = temp; result_pointer = result; } else { result_pointer->next = temp; result_pointer = result_pointer->next; } if (l1 != NULL && l1->next != NULL) l1 = l1->next; else l1 = NULL; if (l2 != NULL && l2->next != NULL) l2 = l2->next; else l2 = NULL; if (l1 == NULL && l2 == NULL && up == 0) break; } return result; } };
'๐STUDY > ๐ coding test๋๋น' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 59. Spiral Matrix II (0) 2022.01.15 [LeetCode] 328. Odd Even Linked List (0) 2022.01.15 [๋ฐฑ์ค] 1158๋ฒ ์์ธํธ์ค ๋ฌธ์ (0) 2020.02.08 ๋๊ธ