문제를 이해하고 있다면 바로 풀이를 보면 됨
전체 코드로 바로 넘어가도 됨
마음대로 번역해서 오역이 있을 수 있음
단일 연결 리스트인 head가 주어졌을 때, 리스트를 반전하고 반전된 리스트를 반환해라.
#1
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
#2
Input: head = [1, 2]
Output: [2, 1]
#3
Input: head = []
Output: []
/**
* 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 node = null;
while(head != null){
ListNode next = head.next;
head.next = node;
node = head;
head = next;
}
return node;
}
}