Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Question
- Given a linked list, reverse it iteratively.
- You don't need to print the elements, just reverse the LL duplicates and return the head of updated LL.
- Input format : Linked list elements (separated by space and terminated by -1)`
- Sample Input 1 :
- 1 2 3 4 5 -1
- Sample Output 1 :
- 5 4 3 2 1
- */
- //Solution
- /*
- class LinkedListNode<T> {
- public T data;
- public LinkedListNode<T> next;
- public LinkedListNode(T data) {
- this.setData(data);
- this.next = null;
- }
- public T getData() {
- return data;
- }
- public void setData(T data) {
- this.data = data;
- }
- }
- * */
- public class Solution {
- public static void printReverseRecursive(LinkedListNode<Integer> head) {
- if (head == null) {
- return;
- }
- printReverseRecursive(head.next);
- System.out.print(head.data + " ");//PRINTING WHILE FALLING DOWN THE STACK
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement