You are viewing a single comment's thread. Return to all comments →
i have done without recurrsion:
void ReversePrint(Node head) {
Node node=head; Node node1=head; int c=0; while(node!=null) { c++; node=node.next; } //System.out.println(c); int arr[]=new int[c]; int i=0; while(node1!=null) { arr[i]=node1.data; i++; node1=node1.next; } int j; if(c>0) { for(j=(c-1);j>=0;j--) { System.out.println(arr[j]); } }
}
same here in C#:
**
public static void ReversePrint(Node head) { if (head != null) { var tempNode = head; var counter = 1; while (tempNode.Next != null) { ++counter; tempNode = tempNode.Next; } int[] arr = new int[counter]; counter = 1; tempNode = head; while (tempNode.Next != null) { arr[counter - 1] = tempNode.Data; ++counter; tempNode = tempNode.Next; } arr[counter-1] = tempNode.Data; for (int i = arr.Length - 1; i >= 0; --i) { Console.WriteLine(arr[i]); } } }
if (head != null) { ReversePrint(head.Next); Console.WriteLine(head.Data); }
Using ArrayList :
static void reversePrint(SinglyLinkedListNode head) { SinglyLinkedListNode temp; ArrayList cars = new ArrayList(); temp = head; while(temp!=null) { cars.add(temp.data); temp = temp.next; } for(int i = cars.size()-1; i>=0; i--) { System.out.println(cars.get(i)); } }
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Print in Reverse
You are viewing a single comment's thread. Return to all comments →
i have done without recurrsion:
void ReversePrint(Node head) {
}
same here in C#:
**
**
if (head != null) { ReversePrint(head.Next); Console.WriteLine(head.Data); }
Using ArrayList :
static void reversePrint(SinglyLinkedListNode head) { SinglyLinkedListNode temp; ArrayList cars = new ArrayList(); temp = head; while(temp!=null) { cars.add(temp.data); temp = temp.next; } for(int i = cars.size()-1; i>=0; i--) { System.out.println(cars.get(i)); } }