[Leetcode] 2816. Double a Number Represented as a Linked List
[Leetcode] 2816. Double a Number Represented as a Linked List

[Leetcode] 2816. Double a Number Represented as a Linked List

์–ธ์–ด
Python
C
๋‚œ์ด๋„
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•
์ž‘์„ฑ์ž
๋ฐ•์šฉ์„ฑ๋ฐ•์šฉ์„ฑ
์ƒ์„ฑ ์ผ์‹œ
2024๋…„ 06์›” 02์ผ
ย 

๐Ÿ–ฅ๏ธย ์‹œ์ž‘ํ•˜๋ฉฐ

๊ฐ„๋‹จํ•œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋ฌธ์ œ๋‹ค.
ย 
  1. ๋ฐ›์€ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์—์„œ ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ์ €์žฅ
  1. ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ฐ”๊พผ ํ›„, 2๋ฐฐ
  1. ๊ฒฐ๊ณผ๋ฌผ์„ ๋‹ค์‹œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅ
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]: if not head: return None # ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋จผ์ € ๋ณ€ํ™˜ text = "" cur = head while cur: text += str(cur.val) cur = cur.next # ๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•œ ๋‹ค์Œ 2๋ฅผ ๊ณฑํ•จ doubled_num = int(text) * 2 # ๊ณฑํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‹ค์‹œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜ # ๊ฒฐ๊ณผ ์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ ๋‹ค์Œ ๊ฐ ์ž๋ฆฟ์ˆ˜๋ฅผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋…ธ๋“œ๋กœ dummy_head = ListNode() cur = dummy_head for char in str(doubled_num): cur.next = ListNode(int(char)) cur = cur.next return dummy_head.next
..๋ผ๊ณ  ํ•˜๋ฉด, ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค!
ย 
ํ…Œ์ŠคํŠธ์ผ€์ด์Šค์—์„œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๊ต‰์žฅํžˆ ๊ธด ์ผ€์ด์Šค๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋ ‡๊ฒŒ 2๋ฅผ ๊ณฑํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ๋Š” ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ์ผ์–ด๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธด๋‹ค.
ย 
๊ทธ๋ ‡๋‹ค๋ฉด, ์šฐ์„  ๋ฆฌ์ŠคํŠธ์˜ ์ˆœ์„œ๋ฅผ ๋’ค์ง‘์€ ํ›„ Carry ๋ฅผ ์ด์šฉํ•ด ๊ณฑํ•˜๊ธฐ์˜ ๊ฒฐ๊ณผ๋ฅผ ์ „๋‹ฌํ•ด ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
ย 

โš™๏ธย Python

class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: prev = None cur = head while cur: next = cur.next cur.next = prev prev = cur cur = next return prev def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]: if not head: return None # ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๋’ค์ง‘๊ธฐ head = self.reverseList(head) cur = head carry = 0 # ๊ฐ ๋…ธ๋“œ์˜ ๊ฐ’์„ ๋‘ ๋ฐฐ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. while cur: doubled_value = cur.val * 2 + carry cur.val = doubled_value % 10 carry = doubled_value // 10 # carry๊ฐ€ ๋‚จ์•„ ์žˆ๋Š”๋ฐ ๋…ธ๋“œ๊ฐ€ ๋์ด๋ฉด ํ•˜๋‚˜ ์ถ”๊ฐ€ if not cur.next and carry: cur.next = ListNode(carry) break cur = cur.next # ๋‹ค์‹œ ๋’ค์ง‘๊ธฐ return self.reverseList(head)

โš™๏ธย C++

class Solution { public: // ๋ฆฌ์ŠคํŠธ ๋’ค์ง‘๊ธฐ ํ•จ์ˆ˜ ListNode* reverseList(ListNode* head) { ListNode *prev = nullptr; ListNode *cur = head; while (cur != nullptr) { ListNode *next = cur->next; cur->next = prev; prev = cur; cur = next; } return prev; } // ๋ฆฌ์ŠคํŠธ ๋‘ ๋ฐฐ๋กœ ๋งŒ๋“ค๊ธฐ ํ•จ์ˆ˜ ListNode* doubleIt(ListNode* head) { if (head == nullptr) { return nullptr; } // ๋ฆฌ์ŠคํŠธ ๋’ค์ง‘๊ธฐ head = reverseList(head); ListNode *cur = head; int carry = 0; // ๋…ธ๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ’์„ ๋‘ ๋ฐฐ๋กœ ๋งŒ๋“ค๊ธฐ while (cur != nullptr) { int doubled_value = cur->val * 2 + carry; cur->val = doubled_value % 10; carry = doubled_value / 10; // ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์ด๊ณ  ์บ๋ฆฌ๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด ์ƒˆ ๋…ธ๋“œ ์ถ”๊ฐ€ if (cur->next == nullptr && carry) { cur->next = new ListNode(carry); break; } cur = cur->next; } // ๋‹ค์‹œ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋’ค์ง‘์–ด ์›๋ž˜ ์ˆœ์„œ๋กœ ๋ณต๊ตฌ return reverseList(head); } };
ย 
ย 

๐Ÿ“Œย ์†Œ๊ฐ

๋’ค์ง‘๋Š”๋‹ค๋Š” ๋ฐœ์ƒ์„ ๋– ์˜ฌ๋ฆฌ๋Š” ๊ฒŒ ์‚ด์ง ์–ด๋ ค์› ๋‹ค.
ย 

๐Ÿ”ย ๋ถ€๋ก

๐Ÿ”ย ์ฐธ๊ณ ๋ฌธํ—Œ


ย 

๋Œ“๊ธ€

guest