public class List {
public static class Node
{
public int value;
public Node next;
public Node(int data)
{
value=data;
}
}
public static class DoubleNode
{
public int value;
public DoubleNode last;
public DoubleNode next;
public DoubleNode(int data)
{
value=data;
}
}
public static Node reverseLinkedList(Node head)
{//单链表找到新的逆序后的头部
Node pre=null;
Node next=null;
while (head!=null)//可达
{
next=head.next;
head.next=pre;
pre=head;
head=next;
}
return pre;
}
public static DoubleNode reversDoubleList(DoubleNode head)
//双链表逆序
{
DoubleNode pre=null;
DoubleNode next=null;
while (head!=null)
{
next=head.next;
head.next=pre;
head.last=next;
pre=head;
head=next;
}
return pre;
}
public static void main(String[] args) {
Node n1=new Node(1);
n1.next=new Node(2);
n1.next.next=new Node(3);
n1=reverseLinkedList(n1);
while(n1!=null)
{
System.out.println(n1.value+" ");
n1=n1.next;
}
}
}
算法:java 单链表多链表逆序
最新推荐文章于 2024-11-15 20:16:11 发布
本文介绍了Java中单链表和双链表的逆序操作,包括`reverseLinkedList`和`reversDoubleList`方法,以及在`main`函数中的应用实例。
摘要由CSDN通过智能技术生成