HackerRank Print In Reverse Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Here's the Linear iterative way with C++

void head) {

auto* cur = head;
SinglyLinkedListNode* prev = NULL;

while(cur){
auto* nxtNode = cur->next;

cur->next = prev;
prev = cur;
cur = nxtNode;
}
head = prev;

auto* cur1 = head;
while (cur1) {
cout << cur1->data << endl;
cur1 = cur1->next;
}
}

Theb
Автор

Hi Nick how about we just reverse a linked list first then do a normal traversal, I know it will cost us in time but we're trading off memory

sphamandlambuyazi
Автор

hi man how can i contact you ? your all social link is block i see

jsinovationtech
Автор

Hey
Here's my approach using a list, I used this since I don't know stacks yet.
if (head == null) {
            return;
        }

while (head != null) {
        list.add(head);
        head = head.next;
        }
int size = list.size()-1;
while (size >= 0) {

       size--;
}
return;

aflah
Автор

A guy did it with recursion. Can you please explain this code ?


void ReversePrint(Node head) {

if(head == null)
;
else{
ReversePrint(head.next);

}

}

arnavphatkar
Автор

here is my c++ implementation (recursive)

if (head == nullptr) { return; }
else {
reversePrint(head->next);
cout << head->data << endl;
return;
}

enarcs
Автор

i copied your code but that doesnt work

pktipsy
Автор

i just reversed the linked list than print it for the previous node so this way previous node is the new head.

foreducation