Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Counting
- Matrix
- Stack
- two pointers
- Number Theory
- Binary Tree
- sorting
- Math
- simulation
- implement
- hash table
- Data Structure
- Class
- greedy
- Binary Search
- Method
- 코테
- 구현
- Tree
- string
- dynamic programming
- 파이썬
- java
- SQL
- geometry
- array
- 자바
- database
- bit manipulation
- 코딩테스트
Archives
- Today
- Total
코린이의 소소한 공부노트
[LeetCode/Easy] 206. Reverse Linked List 본문
1. Input
1) ListNode head
2. Output
1) 연결 리스트를 뒤집은 결과를 반환
3. Constraint
1) 노드의 수의 범위는 [0, 5000]이다.
2) -5000 <= Node.val <= 5000
4. Example
Input: head = [1,2,3,4,5] -> Output: [5,4,3,2,1]
5. Code
1) 첫 코드(2023/05/15)
/**
* 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 {
public ListNode reverseList(ListNode head) {
Stack<Integer> stack = new Stack<>();
while(head!=null){
stack.push(head.val);
head = head.next;
}
ListNode ans = stack.empty() ? null : new ListNode(stack.pop());
ListNode cur = ans;
while(!stack.empty()){
cur.next = new ListNode(stack.pop());
cur = cur.next;
}
return ans;
}
}
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[LeetCode/Easy] 349. Intersection of Two Arrays (0) | 2023.05.18 |
---|---|
[백준 온라인 저지] 11653. 소인수분해 (0) | 2023.05.16 |
[LeetCode/Easy] 169. Majority Element (0) | 2023.05.13 |
[LeetCode/Easy] 145. Binary Tree Postorder Traversal (0) | 2023.05.12 |
[LeetCode/Easy] 144. Binary Tree Preorder Traversal (0) | 2023.05.12 |