单向链表和双向链表
单链表:值,一条next指针
双链表:值,一条last指针,一条next指针
反转代码实现:
public class Code_ReverseList {
/**
* 单链表
*/
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;
}
}
/**
* 单链表逆序
* @param head
* @return
*/
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;
}
/**
* 双链表逆序

本文探讨了单链表和双链表的基本结构,特别关注它们的反转过程。通过介绍单链表(包含值和next指针)和双链表(包含值、last指针及next指针),文章提供了链表反转的代码实现。
最低0.47元/天 解锁文章
1735

被折叠的 条评论
为什么被折叠?



