File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Stack ;
2+
3+ class Solution {
4+ /**
5+ * ๋ฆฌ๋ฒ์ค์ ๋์ผํ ํ์
์ ์ถ ๊ตฌ์กฐ๋ฅผ ์๊ฐํ๊ณ ๊ทธ๋์ ์๋ฃ๊ตฌ์กฐ๋ก ์คํ์ ๊ฒฐ์ ํ๋ค.
6+ * ์ฌ์ดํด ๋ฌธ์ ๋ฅผ ์ผ๋ํด ๋๊ณ ์กฐ๊ฑด์ ์ค๊ณํ์ง๋ง ์ฌ์ดํด์ ๋๋ ์์น๊ฐ ํ๋ ค์ ์ค๋ ๊ฑธ๋ ธ๋ค.
7+ */
8+ public ListNode reverseList (ListNode head ) {
9+ if (head == null ) return null ;
10+
11+ Stack <ListNode > stack = new Stack <>();
12+ while (head != null ) {
13+ stack .push (head );
14+ head = head .next ;
15+ }
16+
17+ ListNode newHead = stack .pop (); // ์ ๋ต ๋ฐํ์ฉ
18+ ListNode current = newHead ; // ํฌ์ธํฐ๋ฅผ ๊ฐฑ์ ํ๋ฉด์ ๊ณ์ฐํ๊ธฐ์ฉ (์์ ๋๋ฏธ ํฌ์ธํฐ)
19+
20+ while (!stack .isEmpty ()) {
21+ current .next = stack .pop ();
22+ current = current .next ; // โ ์์ : ์๊ธฐ ์์ ๊ฐ๋ฆฌํค๋ ๋์ ์์ผ๋ก ์ด๋
23+ }
24+ current .next = null ; // ๋ง์ง๋ง tail ์ ๋ฆฌ
25+ return newHead ;
26+ }
27+ }
You canโt perform that action at this time.
0 commit comments