Answers for "reverse linkedlist in java"

C++
3

reverse linked list in java to get both head and tail

/*
public class ListNode {
    public int val;
    public ListNode next;
    public ListNode(int x) { val = x; next = null; }
}
*/

public static ListNode[] reverse_linked_list(ListNode head) {

        ListNode prev = null;
        ListNode current = head;
        ListNode next;

        ListNode tail = head;

        while (current != null) {

            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }

        head = prev;

        ListNode[] result = {head, tail};

        return result;
}
Posted by: Guest on May-26-2020
-1

reverse linkedlist

Collections.reverse(list);
Posted by: Guest on May-13-2021

Browse Popular Code Answers by Language