Posts Reverse a linked list (geeksforgeeks - SDE Sheet)
Post
Cancel

Reverse a linked list (geeksforgeeks - SDE Sheet)

PROBLEM DESCRIPTION

Given the head of a linked list, the task is to reverse this list and return the reversed head.

geeksforgeeks

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {

    Node reverseList(Node head) {

        Node h1 = head;
        Node h2 = null;

        Node t = head;

        while(h1 != null){

            h1 = h1.next;

            t.next = h2;
            h2 = t;
            t = h1;

        }

        return h2;

    }

}
This post is licensed under CC BY 4.0 by the author.