reverse of a linked list
Anonymous
using recursion class Node { int data; Node next; Node(int data) { this.data=data; next=null; } } class ReverseLLRecursion { static Node reverse(Node head) { if(head.next==null) return head; reverse(head.next).next=head; head.next=null; return head; } public static void main(String[] st) { Node n1=new Node(1); Node n2=new Node(2); Node n3=new Node(3); Node n4=new Node(4); n1.next=n2; n2.next=n3; n3.next=n4; reverse(n1); Node tra=n4; while(tra!=null) { System.out.println(tra.data+", "); tra=tra.next; } } }
Check out your Company Bowl for anonymous work chats.