Skip to content

Commit

Permalink
Solution Merge Two Sorted Lists
Browse files Browse the repository at this point in the history
  • Loading branch information
river20s authored Dec 29, 2024
1 parent 03ded85 commit cf23f1e
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions merge-two-sorted-lists/river20s.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
/*
* [ํ’€์ด]
* 1) ๋‘ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ด๋ฏธ ์ •๋ ฌ๋œ ์ƒํƒœ โ†’ ๋งจ ์•ž์—์„œ๋ถ€ํ„ฐ ๋‘˜ ์ค‘ ๋” ์ž‘์€ ๋…ธ๋“œ๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ rs์— ์ด์–ด ๋ถ™์ธ๋‹ค.
* 2) ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋๋‚  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. โ†’ ๋‚จ์€ ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋“œ๋Š” ๊ทธ๋Œ€๋กœ rs์— ๋ถ™์ธ๋‹ค.
* 3) rs ํ—ค๋“œ ๋…ธ๋“œ์˜ next๋ถ€ํ„ฐ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
* [T.C]
* ๊ฐ ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋น„๊ต โ†’ ์—ฐ๊ฒฐ(๋˜๋Š” ์—ฐ๊ฒฐ๋งŒ)ํ•˜๋ฏ€๋กœ,
* T.C = O(n+m) (n = list1์˜ ๊ธธ์ด, m = list2์˜ ๊ธธ์ด)
* [S.C]
* list1๊ณผ list2์˜ ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ถ”๊ฐ€์ ์ธ ๊ณต๊ฐ„์€ ๊ฑฐ์˜ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ(rs์˜ head ์ •๋„?),
* S.C = O(1)
*/

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode rs = new ListNode(0);
ListNode rsNext = rs;
// step 1
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
rsNext.next = list1;
list1 = list1.next;
}
else {
rsNext.next = list2;
list2 = list2.next;
}
rsNext = rsNext.next;
}
// step 2
if (list1 != null) rsNext.next = list1;
if (list2 != null) rsNext.next = list2;
// step3
return rs.next;
}
}
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
/*
* [ํ’€์ด]
* 1) ๋‘ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ด๋ฏธ ์ •๋ ฌ๋œ ์ƒํƒœ โ†’ ๋งจ ์•ž์—์„œ๋ถ€ํ„ฐ ๋‘˜ ์ค‘ ๋” ์ž‘์€ ๋…ธ๋“œ๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ rs์— ์ด์–ด ๋ถ™์ธ๋‹ค.
* 2) ํ•˜๋‚˜์˜ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋๋‚  ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค. โ†’ ๋‚จ์€ ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋“œ๋Š” ๊ทธ๋Œ€๋กœ rs์— ๋ถ™์ธ๋‹ค.
* 3) rs ํ—ค๋“œ ๋…ธ๋“œ์˜ next๋ถ€ํ„ฐ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
* [T.C]
* ๊ฐ ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์”ฉ ๋น„๊ต โ†’ ์—ฐ๊ฒฐ(๋˜๋Š” ์—ฐ๊ฒฐ๋งŒ)ํ•˜๋ฏ€๋กœ,
* T.C = O(n+m) (n = list1์˜ ๊ธธ์ด, m = list2์˜ ๊ธธ์ด)
* [S.C]
* list1๊ณผ list2์˜ ๋…ธ๋“œ๋ฅผ ๋‹ค์‹œ ์—ฐ๊ฒฐํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ถ”๊ฐ€์ ์ธ ๊ณต๊ฐ„์€ ๊ฑฐ์˜ ํ•„์š”ํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ(rs์˜ head ์ •๋„?),
* S.C = O(1)
*/

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode rs = new ListNode(0);
ListNode rsNext = rs;
// step 1
while (list1 != null && list2 != null) {
if (list1.val <= list2.val) {
rsNext.next = list1;
list1 = list1.next;
}
else {
rsNext.next = list2;
list2 = list2.next;
}
rsNext = rsNext.next;
}
// step 2
if (list1 != null) rsNext.next = list1;
if (list2 != null) rsNext.next = list2;
// step3
return rs.next;
}
}

0 comments on commit cf23f1e

Please sign in to comment.