Posts Reverse a LinkedList
Post
Cancel

Reverse a LinkedList

PROBLEM DESCRIPTION

Reverse a given LinkedList. Reverse a LinkedList

SOLUTION

ITERATIVE APPROACH

snapshot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
    public ListNode reverseList(ListNode head) {
        
        if(head == null) return null;
        
        ListNode p=null,c=head, n=head.next;
        
        while(n!=null){
            c.next=p;
            p=c;
            c=n;
            n=n.next;
        }
        
        c.next=p;
        head = c;
        return head;
        
    }
}
This post is licensed under CC BY 4.0 by the author.