Java language implementation of reverse linked list code example

Problem description

Define a function, input the head node of a linked list, invert the linked list and output the head node of the inverted linked list. The linked list nodes are as follows:

Idea 1:

To reverse the linked list, for node i, we need to point its next to its forward trend, so we need to save the forward trend node. At the same time, if we have reassigned the next of I, we will not be able to find the successor of I. therefore, we need to save the successor of I before reassigning.

code:

Idea 2:

Use the idea of recursion (I didn't think of it at the moment, because if recursion is used, it should be: the first node of the linked list < -- the tail pointer of the recursive returned linked list, but in this case, the inverted head pointer cannot be obtained.) think about it later.

summary

The above is all about the Java language implementation of the reverse linked list code example. Interested friends can refer to: Java programming to delete the repeated nodes in the linked list, solution ideas and source code sharing, Java programming to print the linked list code example from end to end, and other related topics on this site. I hope it will be helpful to you. If there are deficiencies, please leave a message to point out.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>