


Detailed explanation of single chain reversal using recursion and non-recursion
This article describes how to achieve single-chain inversion using recursion and non-recursion. There may be many students who don’t know much about what single-chain inversion is, so let’s cut the nonsense and go directly Read this article!
The question gives hints that both recursive and iterative algorithms can be used.
Because I don’t have a deep understanding of recursion, I first use iteration to write the algorithm. The head node in the question is considered to be the first node containing data
Question idea: start from the head node, traverse backward, and follow the order The reversal of the chains between nodes is gradually realized one by one.
First try to use 2 pointers to complete the operation, but fail
class Solution { public: ListNode* reverseList(ListNode* head) { ListNode* pre = NULL; while(head -> next) { pre = head; head = head -> next; head -> next = pre; } return head; };
The reason is as follows: During the reversal process, head ->next has been reversed and cannot continue to After traversing, so adding a pointer and using 3 pointers to complete the operation
class Solution { public: ListNode* reverseList(ListNode* head) { ListNode* pre = NULL; while(head) { ListNode* next = head -> next; head -> next = pre;//head是尾结点,指针置空 head = next; pre = head; } return pre; //head = NULL };
recursion will not happen. Please refer to Discuss to understand.
The idea is as follows: through recursion, the head node is continuously traversed backwards until the termination condition: stops after reaching the tail node.
The code is as follows:
class Solution { public: ListNode* reverseList(ListNode* head) { //终止条件,达到尾结点停止 if(head == NULL || head ==NULL) return head; else { //头结点向后移动 ListNode* temp = reverList(head->next); head->next->next = head; //1 head->next = NULL; //2 } return temp; };
The status presented when the recursive termination condition is reached is as follows:
The status after returning to the previous level of recursion is as follows:
AfterCommentsAfter the two lines of code 1 and 2, the status is as follows
It can be seen that the position of the head node is related to the number of recursion levels, and temp is The position of the reversed head node never moves, thus achieving the reversal effect.
Although I can understand the recursive code, I don't know how to design it yet, and I am still a novice.
##Related articles:
PHP implementation Single chainTable flip operation example
The above is the detailed content of Detailed explanation of single chain reversal using recursion and non-recursion. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The recursion depth of C++ functions is limited, and exceeding this limit will result in a stack overflow error. The limit value varies between systems and compilers, but is usually between 1,000 and 10,000. Solutions include: 1. Tail recursion optimization; 2. Tail call; 3. Iterative implementation.

Yes, C++ Lambda expressions can support recursion by using std::function: Use std::function to capture a reference to a Lambda expression. With a captured reference, a Lambda expression can call itself recursively.

How to implement dual WeChat login on Huawei mobile phones? With the rise of social media, WeChat has become one of the indispensable communication tools in people's daily lives. However, many people may encounter a problem: logging into multiple WeChat accounts at the same time on the same mobile phone. For Huawei mobile phone users, it is not difficult to achieve dual WeChat login. This article will introduce how to achieve dual WeChat login on Huawei mobile phones. First of all, the EMUI system that comes with Huawei mobile phones provides a very convenient function - dual application opening. Through the application dual opening function, users can simultaneously

The programming language PHP is a powerful tool for web development, capable of supporting a variety of different programming logics and algorithms. Among them, implementing the Fibonacci sequence is a common and classic programming problem. In this article, we will introduce how to use the PHP programming language to implement the Fibonacci sequence, and attach specific code examples. The Fibonacci sequence is a mathematical sequence defined as follows: the first and second elements of the sequence are 1, and starting from the third element, the value of each element is equal to the sum of the previous two elements. The first few elements of the sequence

The recursive algorithm solves structured problems through function self-calling. The advantage is that it is simple and easy to understand, but the disadvantage is that it is less efficient and may cause stack overflow. The non-recursive algorithm avoids recursion by explicitly managing the stack data structure. The advantage is that it is more efficient and avoids the stack. Overflow, the disadvantage is that the code may be more complex. The choice of recursive or non-recursive depends on the problem and the specific constraints of the implementation.

How to implement the WeChat clone function on Huawei mobile phones With the popularity of social software and people's increasing emphasis on privacy and security, the WeChat clone function has gradually become the focus of people's attention. The WeChat clone function can help users log in to multiple WeChat accounts on the same mobile phone at the same time, making it easier to manage and use. It is not difficult to implement the WeChat clone function on Huawei mobile phones. You only need to follow the following steps. Step 1: Make sure that the mobile phone system version and WeChat version meet the requirements. First, make sure that your Huawei mobile phone system version has been updated to the latest version, as well as the WeChat App.

A recursive function is a technique that calls itself repeatedly to solve a problem in string processing. It requires a termination condition to prevent infinite recursion. Recursion is widely used in operations such as string reversal and palindrome checking.

In today's software development field, Golang (Go language), as an efficient, concise and highly concurrency programming language, is increasingly favored by developers. Its rich standard library and efficient concurrency features make it a high-profile choice in the field of game development. This article will explore how to use Golang for game development and demonstrate its powerful possibilities through specific code examples. 1. Golang’s advantages in game development. As a statically typed language, Golang is used in building large-scale game systems.
