-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eunhwa99
committed
Jan 18, 2025
1 parent
a26f8ee
commit 21586c4
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* 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; } | ||
* } | ||
*/ | ||
|
||
// 시간 복잡도: O(N) | ||
// 공간복잡도: O(N) | ||
class Solution { | ||
public ListNode reverseList(ListNode head) { | ||
if(head==null) return head; | ||
|
||
ListNode pointer = new ListNode(head.val); | ||
|
||
ListNode tempPointer; | ||
while(head.next!=null){ | ||
tempPointer = new ListNode(head.next.val, pointer); | ||
pointer = tempPointer; | ||
head = head.next; | ||
} | ||
} | ||
return pointer; | ||
} | ||
|