Example:

``````Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
``````

## Solution

### iterative

``````public ListNode reverseList(ListNode head) {
ListNode prev = null;
}
return prev;
}
``````
``````public ListNode reverseList(ListNode head) {
ListNode prev = null;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
``````

### recursive

``````public ListNode reverseList(ListNode head) {
return p;
}
``````
``````    ListNode newHead;
return null;
}
}
ListNode reverseUtil(ListNode curr, ListNode prev) {

/* If last node mark it head*/
if (curr.next == null) {

/* Update next to prev node */
curr.next = prev;

}

/* Save curr->next node for recursive call */
ListNode next = curr.next;

/* and update next ..*/
curr.next = prev;

reverseUtil(next, curr);
}
``````

Reference Code

``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
class Solution {
}
}
}
}
``````

Using Dummy Node Version

``````class Solution {
return null;
}
ListNode dummy = new ListNode(0);
while (p.next != null) {
ListNode q = p.next;
p.next = q.next;
q.next = dummy.next;
dummy.next = q;
}
return dummy.next;
}
}
``````