Java 程序:成对反转链表
原文:https://www.studytonight.com/java-programs/java-program-to-reverse-a-linked-list-in-pairs
在本教程中,我们将看到如何在 java 中成对反转链表。LinkedList 是一种线性数据结构,其中元素不存储在连续的位置,每个元素都是一个单独的对象,具有数据部分和地址部分。每个元素都被称为一个节点。由于插入和删除的动态性和容易性,它们比数组更受欢迎。但是在继续之前,如果你不熟悉 java 中链表的概念,那么一定要查看一下 Java 中链表上的文章。
输入:输入链表元素:6 7 8 4 5 3
输出:反转链表前:6 7 8 9 5
反向链表后:7 6 9 8 3 5
这可以通过使用以下方法来实现:
方法 1:通过迭代
方法 2:通过递归
让我们看看这些方法中的每一种,以便更好地理解。
Java 程序:程序 1:成对反转链表
在这个程序中,我们将看到如何使用迭代方法在 java 中成对反转链表。
算法:
- 开始
- 声明没有任何初始大小的
Integer
类型的链表。 - 使用 add 方法添加元素。
- 将元素追加到列表的末尾。
- 冲销前打印链表元素。
- 首先正确链接两个节点,然后交换这些节点。
- 由于之前的链接断开,现在再次链接节点。
- 反转后打印链表元素。
- 停止
让我们看看下面的例子,以更好地理解上述算法。
//Java Program to Reverse a linked list in Pairs
public class ReverseLinkedListInPair
{
private Node head;
private static class Node
{
private int value;
private Node next;
Node(int value) {
this.value = value;
}
}
public void addLast(Node node) {
if (head == null) {
head = node;
} else {
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = node;
}
}
public void printLinkedList(Node head) {
Node temp = head;
while (temp != null) {
System.out.format("%d ", temp.value);
temp = temp.next;
}
System.out.println();
}
// Reverse the linked list in pair
public static Node reverseLLInPairs(Node head)
{
Node current=head;
Node temp=null;
Node newHead =null;
while (current != null && current.next != null)
{
if (temp != null)
{
temp.next.next = current.next;
}
temp=current.next;
current.next=temp.next;
temp.next=current;
if (newHead == null)
newHead = temp;
current=current.next;
}
return newHead;
}
//Driver Code
public static void main(String[] args) {
ReverseLinkedListInPair li = new ReverseLinkedListInPair();
// Creating a linked list
Node head=new Node(0);
li.addLast(head);
li.addLast(new Node(1));
li.addLast(new Node(2));
li.addLast(new Node(3));
li.addLast(new Node(4));
li.addLast(new Node(5));
System.out.println("Before reversing in pair: ");
li.printLinkedList(head);
//Reversing LinkedList in pairs
Node result=reverseLLInPairs(head);
System.out.println("After reversing in pair: ");
li.printLinkedList(result);
}
}
成对倒车前: 0 1 2 3 4 5 成对倒车后: 1 0 3 2 5 4
Java 程序:程序 2:成对反转链表
在这个程序中,我们将看到如何使用递归方法在 java 中成对反转链表。
算法:
- 开始
- 声明没有任何初始大小的
Integer
类型的链表。 - 使用 add 方法添加元素。
- 将元素追加到列表的末尾。
- 冲销前打印链表元素。
- 首先正确链接两个节点,然后调用递归函数来交换元素。
- 由于之前的链接断开,现在再次链接节点。
- 反转后打印链表元素。
- 停止
让我们看看下面的例子,以更好地理解上述算法。
//Java Program to Reverse a linked list in Pairs
public class ReverseLinkedListInPair
{
private Node head;
private static class Node
{
private int value;
private Node next;
Node(int value) {
this.value = value;
}
}
public void addLast(Node node) {
if (head == null) {
head = node;
} else {
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = node;
}
}
public void printLinkedList(Node head) {
Node temp = head;
while (temp != null) {
System.out.format("%d ", temp.value);
temp = temp.next;
}
System.out.println();
}
// Reverse the linked list in pair
public static Node reverseLLInPairs(Node head)
{
if (head == null || head.next == null) {
return head;
}
Node temp=head.next;
head.next=temp.next;
temp.next=head;
head.next=reverseLLInPairs(head.next);
return temp;
}
//Driver Code
public static void main(String[] args) {
ReverseLinkedListInPair li = new ReverseLinkedListInPair();
// Creating a linked list
Node head=new Node(0);
li.addLast(head);
li.addLast(new Node(1));
li.addLast(new Node(2));
li.addLast(new Node(3));
li.addLast(new Node(4));
li.addLast(new Node(5));
System.out.println("Before reversing in pair: ");
li.printLinkedList(head);
//Reversing LinkedList in pairs
Node result=reverseLLInPairs(head);
System.out.println("After reversing in pair: ");
li.printLinkedList(result);
}
}
成对倒车前: 0 1 2 3 4 5 成对倒车后: 1 0 3 2 5 4