Reversing the single-way linked list以下是在Java中实现单向链表反转的两种常见方法:
**一、迭代法**
1. **思路**
- 需要三个指针,分别表示当前节点(`cur`)、当前节点的前驱节点(`pre`)和当前节点的后继节点(`next`)。
- 初始时,`pre`为`null`,`cur`指向链表的头节点。
- 在遍历链表的过程中,先保存`cur`的后继节点到`next`,然后将`cur`的`next`指针指向`pre`,接着更新`pre`为`cur`,`cur`为`next`,直到`cur`为`null`,此时`pre`就是反转后的链表头节点。
2. **示例代码**
```java
class Node {
public int value;
public Node next;
public Node(int i) {
value = i;
next = null;
}
// 向链表中添加新节点
public void add(Node head, Node add) {
if (head == null) {
return;
}
while (head.next!= null) {
head = head.next;
}
head.next = add;
}
// 由头节点出发顺序遍历整个链表
public static void print(Node head) {
if (head == null) {
System.out.println("链表为空");
}
while (head!= null) {
System.out.print((head.value + " => "));
head = head.next;
}
}
// 反转链表(迭代的方式顺序逐步处理)
public static Node reverse(Node head) {
if (head == null || head.next == null) {
return head;
}
Node cur = head;
Node pre = null;
Node next = null;
while (cur!= null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
```
**二、递归法**
1. **思路**
- 对于递归反转链表,基本思想是将链表除了头节点之外的部分先递归反转,然后将头节点连接到反转后的链表后面。
- 递归的终止条件是当链表只有一个节点或者为空时,直接返回该节点。
2. **示例代码**
```java
class Node {
public int value;
public Node next;
public Node(int i) {
value = i;
next = null;
}
// 递归方法反转链表
public Node reverseRecurrence(Node head) {
if (head.next == null) {
return head;
}
Node last = reverseRecurrence(head.next);
head.next.next = head;
head.next = null;
return last;
}
}
```
<a href="/?from=ask_words" style="color:red" target="_blank">点击前往免费阅读更多精彩小说</a>
A set of linked list reversal skillsHere are some techniques to reverse the list:
** 1. Iteration Reversal (using auxiliary pointers)**
1. ** Three nodes in a group **
- Three auxiliary pointers were set: pre-node, cur-node, and next.
- Initially, pre-is empty (no predecessor for the head node), cur-points to the head node of the list, and next-points to the successor node of cur-points.
- Then, by adjusting the direction of the pointer so that cur-point points to pre-point, and then moving the three nodes back (pre-point = cur-point, cur-point = next, next = next.next), the entire list could be reversed.
2. ** An improved iterated method to avoid node loss (for the case where multiple nodes are reversed, such as reversing some nodes in a linked list)**
- At least 3 references are required (for example, reversing the list node from position left to position right).
- During the reversal process, specify the middle reference (such as the reference of the middle node) to reverse the next pointer of the node. For example, when there are n1 (the predecessor node pointing to the current node to be reversed), n2 (the current node to be reversed), and n3 (the successor node pointing to the current node to be reversed), n1 and n3 can be used to complete the reversal of n2 without losing the subsequent nodes.
- Move references back at the right time. For example, before reversing a node, move references one after another to ensure that the subsequent nodes are not lost.
** 2. Using stacks to achieve reversal **
1. ** Principle **
- The stack was a first-in-last-out data structure.
- The linked list nodes were pushed into the stack in turn, and then they were pushed out of the stack in turn to build a new linked list. This achieved the reversal of the linked list.
- The specific operation was to start from the first node of the linked list and push each node into the stack. After all the nodes were pushed into the stack, the nodes were ejected one by one. The ejected nodes were reconnected into a new linked list, and the new linked list was the result of reversing the original linked list.
- However, this method was not very ideal in terms of performance and memory consumption, so it could be used as a guaranteed solution for the written test.
** 3. Realizing Reversal by Recurring **
1. ** Thoughts **
- For the list reversal method, if the list is empty or there is only one node (head == zero),||<head.next>
- Otherwise, the next node part of the linked list would be reversed first (ListNode reverse = reverseList(head.next)).
- Then, the next pointer of the next node (head.next) of the current node (head) points to the current node (head.next.next = head), and then the next pointer of the current node is assigned to zero (head.next = zero) to avoid circular references. Finally, the inverted head node of the linked list is returned (return reverse).
- The recursion method had a good performance in terms of performance and memory consumption, and the code was simple.
<a href="/?from=ask_words" style="color:red" target="_blank">Read more exciting novels for free</a>
Nine-linked toyThe nine-ring chain was a traditional Chinese folk intelligence toy. It was made of nine rings made of metal wire, which were set on a horizontal plate or various frames, and were connected with a ring handle. The player could unlock the nine rings separately or combine them into one by repeating the operation according to a certain program. The nine-linked chain could be traced back to the Warring States Period. Xu Ke, who was at the end of the Qing Dynasty and the beginning of the Republic of China, recorded the solution to the nine-linked chain in detail in the book "Qing Bai Lei Banknote." The nine-linked chain was not only a child's toy, but also an educational tool that was beneficial to intellectual development. Through the process of solving puzzles, children need to use logical thinking to cultivate patience and perseverance. The nine linked chains were also challenging, regular, and interesting, attracting people's curiosity and desire to conquer. With the development of the times, the Nine Chain Rings had also evolved. The modern Nine Chain Rings not only retained the traditional metal rings, but also had various changes in materials and shapes. There was also an electronic version of the 9-chain game, which added a new element to the puzzle game.
Java novelsAs a fan of web novels, I don't have the ability to actually read novels. However, I can provide you with some novel recommendations on programming in Java.
If you want to read a novel about programming, I recommend you to read the book,"Thoughts on programming with java.com." This book is the autobiography of a Java developer. It tells the author's practical experience and thinking process in the field of Java programming. The book not only covers the core concepts and grammar of the programming language, but also provides an in-depth discussion of the application and optimization methods of the programming language.
Also, if you want to know more about the storyline of the game, I can recommend some novels about the game. For example, you can read the book "The Java Coders", which tells how a group of Java developers challenged various programming problems and developed amazing applications. Also, you can read the book, The Java Development Life Cycle, which talks about the life cycle methods and tools in the development of Java.
I hope these recommendations can meet your needs.
Gala-linked skin exposureJia Luo had many linked skins exposed. The new AOV-linked skin had been released with special effects. When the big move was released, a spiritual fox would appear. The second skill would become a simple special effect. The costume design was R-style, and the quality would be Legendary after it was released. In addition, Jialuo had a linked skin plan in the international server. The linked skin of the same series was at least Epic quality, and the highest was Legendary quality. At the same time, Jialuo's new skin grid marked with the Legend of Sword and Fairy was revealed, and the national server might also have a new linked skin in the near future.
"Luo Mingxia Love Letter" is equally exciting. Everyone is welcome to click and read it!
The meaning of java-reflectionReflection was a mechanism that allowed a program to know all the properties, methods, and other information of any class in the running state. It could also obtain this information and call the methods of the object. It allowed the program to query and manipulate the type information of the object at run time. For example, it could directly manipulate the.class file at run time, and it could manipulate the variables, methods, builders, and other information in the class. The java.lang.reflect package provides a set of classes and interface to implement the functions related to reflection, such as the Class class (which can be used to obtain class information), the Field class (which is used to represent the fields of the class), the Method class (which is used to represent the methods of the class), and the Builder class (which is used to represent the builders of the class). Through reflection, you can create objects, call methods, access and modify fields. Reflection is very useful in scenarios such as framework, tools, or dynamic loading classes. However, since it can bypass some access restrictions and bring about a certain performance overhead, it should be used with caution, follow security regulations, and only use it when necessary.
<a href="/?from=ask_words" style="color:red" target="_blank">Read more exciting novels for free</a>