<reponame>gyan42/interview-preparation-qns
"""
Given the head of a singly linked list, reverse the list, and return the reversed list.

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]

Input: head = [1,2]
Output: [2,1]

Input: head = []
Output: []

Constraints:

    The number of nodes in the list is the range [0, 5000].
    -5000 <= Node.val <= 5000


"""


# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head or not head.next:
            return head

        prev = None
        curr = head
        while (curr):
            nxt = curr.next
            curr.next = prev
            prev = curr
            curr = nxt

        return prev
"""
Given the head of a singly linked list, reverse the list, and return the reversed list.

Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]

Input: head = [1,2]
Output: [2,1]

Input: head = []
Output: []

Constraints:

    The number of nodes in the list is the range [0, 5000].
    -5000 <= Node.val <= 5000


"""


# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next


class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        if not head or not head.next:
            return head

        prev = None
        curr = head
        while (curr):
            nxt = curr.next
            curr.next = prev
            prev = curr
            curr = nxt

        return prev
Given the head of a singly linked list, reverse the list, and return the reversed list.  Input: head = [1,2,3,4,5] Output: [5,4,3,2,1]  Input: head = [1,2] Output: [2,1]  Input: head = [] Output: []  Constraints:      The number of nodes in the list is the range [0, 5000].     -5000 <= Node.val <= 5000 # Definition for singly-linked list. :type head: ListNode         :rtype: ListNode