public ListNode reverseList(ListNode head) {
ListNode result = new ListNode();
ListNode answer = result;
Stack<Integer> stack = new Stack<>();
while(head != null){
stack.push(head.val);
head = head.next;
}
while(!stack.isEmpty()){
int val = stack.pop();
ListNode temp = new ListNode();
temp.val = val;
result.next = temp;
result = result.next;
}
return answer.next;
}
public ListNode reverseList(ListNode head) {
ListNode newNode = null;
while(head != null){
ListNode next = head.next;
head.next = newNode;
newNode = head;
head = next;
}
return newNode;
}
// 1 2 3 4 5
// next = head.next 2 3 4 5 | 3 4 5 | 4 5 | 5 | null
// head.next = newNode null | 1 null | 2 1 null | 3 2 1 null | 4 3 2 1 null
// newNode = head; 1 null | 2 1 null | 3 2 1 null | 4 3 2 1 null 5 4 3 2 1 null
// head = next; 2 3 4 5 | 3 4 5 | 4 5 | 5 | null
이해하면 쉬워보이는데 스스로 하려고 하면 참 어렵다. ㄱㅖ속 보자!