算法思想
- 调用reverse(Node curr)方法反转每一个结点,从元素1结点开始;
- 如果发现curr还有下一个节点,则递归调用reverse(curr,next)对下一个结点反转;
- 最终递归的出口是最后一个元素结点,因为它没有下一个元素了,当到达了出口处让head指向最后一个元素结点;
- 递归依次返回,完成。
具体实现
核心代码:
//返回整个链表
public void reverse(){
//判断当前链表是否是空链表,如果是结束,如果不是调用重载的方法完成反转
if(isEmpty()){
return;
}else{
reverse(head.next);
}
}
//反转指定的结点curr,并把反转后的结点返回
public Node reverse(Node curr){
if(curr.next==null){
head.next = curr;
return curr;
}else{
//递归的反转curr结点的下一个结点
Node pre = reverse(curr.next);
pre.next = curr;
curr.next = null;
return curr;
}
}
完整代码和测试代码:
package linear;
import java.util.Iterator;
public class LinkList<T> implements Iterable<T>{
//记录头结点
private Node head;
//记录链表长度
private int N;
//结点类
private class Node{
//存储数据
T item;
//下一个节点
Node next;
//内部类的构造方法
public Node(T item,Node next){
this.item = item;
this.next = next;
}
}
public LinkList(){
//初始化头结点
this.head = new Node(null,null);
//初始化元素个数
this.N = 0;
}
//清空链表
public void clear(){
this.head.next = null;
this.N = 0;
}
//获取链表长度
public int length(){
return this.N;
}
//判断链表是否为空
public boolean isEmpty(){
return this.N == 0;
}
//获取指定位置i处的元素
public T get(int i){
Node n = head.next;
for(int index=0;index<i;index++){
n = n.next;
}
return n.item;
}
//向链表中添加元素t
public void insert(T t){
//找到当前最后一个节点
Node n = head;
//创建新节点保存元素
while(n.next!=null){
n = n.next;
}
//当前最后一个结点指向新节点
Node newNode = new Node(t,null);
n.next = newNode;
//元素个数加一
N++;
}
//向链表中添加元素t
public void insert(int i,T t){
//找到i位置前一个节点
Node pre = head;
for(int index=0;index<=i-1;index++){
pre = pre.next;
}
//找到i位置的结点
Node curr = pre.next;
//创建新节点
Node newNode = new Node(t,curr);
pre.next = newNode;
N++;
}
//删除指定位置i处的元素,并返回被删除的元素
public T remove(int i){
//找到i位置前一个节点
Node pre = head;
for(int index=0;index<=i-1;i++){
pre = pre.next;
}
Node curr = pre.next;
Node nextNode = curr.next;
pre.next = nextNode;
N--;
return nextNode.item;
}
//查找元素t在链表中第一次出现的位置
public int indexOf(T t){
Node n = head;
for(int i=0;n.next!=null;i++){
n = n.next;
if(n.item.equals(t)){
return i;
}
}
return -1;
}
@Override
public Iterator<T> iterator() {
return new LIterator();
}
private class LIterator implements Iterator{
private Node n;
public LIterator(){
this.n = head;
}
@Override
public boolean hasNext() {
return n.next!=null;
}
@Override
public Object next() {
n = n.next;
return n.item;
}
}
//返回整个链表
public void reverse(){
//判断当前链表是否是空链表,如果是结束,如果不是调用重载的方法完成反转
if(isEmpty()){
return;
}else{
reverse(head.next);
}
}
//反转指定的结点curr,并把反转后的结点返回
public Node reverse(Node curr){
if(curr.next==null){
head.next = curr;
return curr;
}else{
//递归的反转curr结点的下一个结点
Node pre = reverse(curr.next);
pre.next = curr;
curr.next = null;
return curr;
}
}
}
package test;
import linear.LinkList;
public class LinkListTest {
public static void main(String[] args) {
//创建顺序表对象
LinkList<String> s1 = new LinkList<>();
//测试插入
s1.insert("小红");
s1.insert("小兰");
s1.insert("小黄");
s1.insert(1,"小绿");
//遍历元素
for(String s : s1){
System.out.println(s);
}
System.out.println(s1.length());
System.out.println("-------------------------------------");
s1.reverse();
//遍历元素
for(String s : s1){
System.out.println(s);
}
}
}