알고리즘/LinkedList
Leet code (Easy) : 206. Reverse Linked List - JAVA
mezo
2024. 10. 16. 20:20
728x90
반응형
링크드리스트의 개념을 이해하도록 도와주는 문제였다.
/**
* 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) {
ListNode prev = null;
ListNode next = null;
ListNode curr = head;
while(curr != null){
next = curr.next;
// 다음 노드를 가리키는 방향을 반대로 바꿈
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
}
728x90
반응형