class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def reverse_linked_list(head):
    prev = None
    current = head

    while current:
        next_node = current.next
        current.next = prev
        prev = current
        current = next_node

    return prev

# Example usage
head = ListNode(1)
head.next = ListNode(2)
head.next.next = ListNode(3)
head.next.next.next = ListNode(4)
head.next.next.next.next = ListNode(5)

reversed_head = reverse_linked_list(head)

while reversed_head:
    print(reversed_head.val)
    reversed_head = reversed_head.next

# Output:
# 5
# 4
# 3
# 2
# 1

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: