ycliper

Популярное

Музыка Кино и Анимация Автомобили Животные Спорт Путешествия Игры Юмор

Интересные видео

2025 Сериалы Трейлеры Новости Как сделать Видеоуроки Diy своими руками

Топ запросов

смотреть а4 schoolboy runaway турецкий сериал смотреть мультфильмы эдисон
Скачать

How To Reverse A Singly Linked List | The Ultimate Explanation (Iteratively & Recursively)

Back To Back SWE

Software Engineering

programming

programming interviews

coding interviews

coding questions

algorithms

algorithmic thinking

Google internship

Facebook internship

software engineering internship

swe internship

big n companies

reverse a linked list

linked list

recursive

iterative algorithms

recursive algorithms

reverse a singly linked list

Автор: Back To Back SWE

Загружено: 2018-12-18

Просмотров: 167982

Описание: Free 5-Day Mini-Course: https://backtobackswe.com
Try Our Full Platform: https://backtobackswe.com/pricing
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions


This is a classic interview problem for 1st and 2nd year interviews. I got this question in my interview for the Explore Microsoft Program.

Very simple. But we will still do this.

This is what I call a utility problem. Utility problems don't test your ability to think, they test your ability to do things such as traverse or manipulate data structures in often trivial (but not always) ways.

The brute force for this is to copy the linked list into a static structure like an array and then create a linked list from that...nah.

So the 2 ways we will really do it is to move the pointers around

Way 1 (Iterative):

For this approach we need to keep track of a prev and curr node. Why? This list is singly-linked so we can't get a node's predecessor, so during the traversal we must remember who came before the node we are at.

Before doing any of that we save the next node's address since if we move the node we are at's address, we would lose where to go next in the traversal.

Then finally we set the current node to the next node that we saved at the start of the iteration.

Complexities:

Time: O(n)
Space: O(1)

Way 2 (Recursive):

This is "top down" recursion, we start at the top and drill down. Then when we reach the bottom (the base case) we will come back upwards with the answer.

The recursive code will drill down in recursive calls until we get to the base case.

The base case gets triggered when the current node has no next, this means we are at the list's end, in that case we just return the node passed into the call.

Then the node before the last node has itself and the last node to work with.

We do some pointer movement.

Last node points to curr.

curr pointer to null since we don't know if this is the first node in the list.

first node will end up the last node in the list.

Then we return the head of the new linked list on the way back upwards.

The we repeat, what we get from the recursive call is a reversed list, we append our node to that list, and then return back upwards with an even longer reversed list.

In the top call we say:

"Reverse the rest of the list"
"Reverse the rest of the list"
"Reverse the rest of the list"
I'm the last node "Here I am 2nd to last node"
"processing"
3rd to last gets a reversed list, append
4th to last gets a reversed list, append
......and so on

Complexities:

Time: O(n)
Space: O(n)

Call stack space used.

++++++++++++++++++++++++++++++++++++++++++++++++++

HackerRank:    / @hackerrankofficial  

Tuschar Roy:    / tusharroy2525  

GeeksForGeeks:    / @geeksforgeeksvideos  

Jarvis Johnson:    / vsympathyv  

Success In Tech:    / @successintech  

Не удается загрузить Youtube-плеер. Проверьте блокировку Youtube в вашей сети.
Повторяем попытку...
How To Reverse A Singly Linked List | The Ultimate Explanation (Iteratively & Recursively)

Поделиться в:

Доступные форматы для скачивания:

Скачать видео

  • Информация по загрузке:

Скачать аудио

Похожие видео

Merge 2 Sorted Lists - A Fundamental Merge Sort Subroutine (

Merge 2 Sorted Lists - A Fundamental Merge Sort Subroutine ("Merge Two Sorted Lists" on LeetCode)

Reverse Linked List - Iterative AND Recursive - Leetcode 206 - Python

Reverse Linked List - Iterative AND Recursive - Leetcode 206 - Python

Total Occurrences Of K In A Sorted Array (Facebook Software Engineering Interview Question)

Total Occurrences Of K In A Sorted Array (Facebook Software Engineering Interview Question)

Implement A Binary Heap - An Efficient Implementation of The Priority Queue ADT (Abstract Data Type)

Implement A Binary Heap - An Efficient Implementation of The Priority Queue ADT (Abstract Data Type)

L9. Reverse a LinkedList | Iterative and Recursive

L9. Reverse a LinkedList | Iterative and Recursive

Depth First & Breadth First Graph Search - DFS & BFS Graph Searching Algorithms

Depth First & Breadth First Graph Search - DFS & BFS Graph Searching Algorithms

Игра, опередившая время на десятилетия  | The Movies 2005

Игра, опередившая время на десятилетия | The Movies 2005

Похудей на 45 КГ, Выиграй $250,000!

Похудей на 45 КГ, Выиграй $250,000!

The Change Making Problem - Fewest Coins To Make Change Dynamic Programming

The Change Making Problem - Fewest Coins To Make Change Dynamic Programming

Introduction to Linked Lists (Data Structures & Algorithms #5)

Introduction to Linked Lists (Data Structures & Algorithms #5)

© 2025 ycliper. Все права защищены.



  • Контакты
  • О нас
  • Политика конфиденциальности



Контакты для правообладателей: [email protected]