Java implementation of double linked list to exchange any two nodes with each other
This paper describes the method of exchanging any two nodes in a double linked list in Java. Share with you for your reference, as follows:
summary:
Bidirectional linked list, also known as double linked list, is a kind of linked list. There are two pointers in each data node, pointing to the direct successor and direct precursor respectively. Therefore, starting from any node in the two-way linked list, you can easily access its predecessor node and successor node. Generally, we construct a two-way circular linked list.
Idea:
1. Determine the sequence of the two nodes, 2. Exchange the sequence of next and prev with each other, and connect the node in front of the commutation with the previous node. (1. Prev. Next = 2) 3. Judge whether it is adjacent
Implementation code:
Linked list class:
Node class:
Student class:
For more information about Java algorithms, readers who are interested can see the topics on this site: Java data structure and algorithm tutorial, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills