Reverse a Linked List problem LeetCode

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


 


Example 1:



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

Output: [5,4,3,2,1]

Example 2:



Solution :

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
       
ListNode previous=null;
ListNode current=head;
ListNode next = null;

while(current !=null)
{
    next=current.next;

    current.next=previous;
    previous=current;
    current=next;
}
return previous;

    }
}


Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence