When given a singly linked list how can we manage to reverse it? Like the following example, if the input linked list is 1->2->3->4->5->NULL, can we reverse it into the output as 5->4->3->2->1->NULL?
Example Input
Example Output
Each node of Linked list will have two attributes: value & next, and linked list will have head, tail and length attribute.
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
https://gist.github.com/GAierken/6e15c82f5e3457fa2d16e29400e130aa
There are two approaches to reverse a singly linked list: iterative and recursive.
#javascript #recursion #singly-linked-list #reverse-linked-list #iteration