> For the complete documentation index, see [llms.txt](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heunnajo.gitbook.io/algorithms-problem-solving-skills/linked-list/reverse-linkedlist.md).

# Reverse LinkedList

Simply reverse a singly linked list.

> Example :\
> Input : 1->2->3->4->5->NULL\
> Output : 5->4->3->2->1->NULL

알고리즘\
1\. 임시 변수를 만들어 현재 노드의 다음 노드를 저장한다. \
2\. 현재 노드가 이 임시변수를 통해 다음 노드로 이동한다. 현재 노드는 prev가 된다.\
3\. 1\~2를 거치며 현재 노드의 다음 노드를 가리키는 포인터는 prev를 가리키도록 한다.

```java
/**
 * 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 curr = head;
        ListNode prev = null;
        
        while(curr != null) {
            ListNode nextTemp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTemp;
        }
        return prev;//제일 끝 노드를 가리킨다!
        
    }
}
```
