CA 22 - Reverse a Linked List

 

Reverse a linked list

Difficulty: EasyAccuracy: 73.11%Submissions: 384K+Points: 2Average Time: 30m

You are given the head of a singly linked list. You have to reverse the linked list and return the head of the reversed list.

Examples:

Input:
Output: 4 -> 3 -> 2 -> 1 Explanation: After reversing the linkedList

Input: 
Output: 8 -> 9 -> 10 -> 7 -> 2

Explanation: After reversing the linked list

Input: 

Output: 8 Explanation:

Constraints:
1 ≤ number of nodes ≤ 105
1 ≤ node->data ≤ 105







MyCODE:


class Solution:

    def reverseList(self, head):

        prev=None

        curr=head

        while curr:

            n_x =curr.next

            curr.next=prev

            prev=curr

            curr=n_x

        return prev

Comments

Popular posts from this blog