实现链表的逆序的三种方式

问题
  • 给定一个带头节点的单链表,将其逆序。head->1->2->3->4->5->6->7变为 head->7->6->5->4->3->2->1。
分析

单链表与数组不同,单链表中每个节点的地址都存储在其前驱节点的指针域中,因此对链表中任何一个节点的访问只能从链表的头指针开始进行遍历。在修改节点指针域的时候,记录后继节点的地址,以防丢失后继节点。

注:在之前的文章通过java实现了单链表,我们以此具体链表为例,添加倒叙操作。点击此连接跳转查看具体实现

方法一:就地逆序
  • 在遍历列表时,修改当前节点的指针域指向,让其指向它的前驱节点。因此,需要一个变量来记录当前节点,以防后继节点消失,还需要一个变量保存后继节点,除此之外还需要对首位节点进行处理。算法实现如下:逆序是最下面的 reverse() 方法

public class TextLink<T> {

    private class Node {//存储数据和next节点
        private Object data;
        Node next;

        public Node() {
            data = null;
        }

        public Node(T data) {
            this.data = data;
        }
    }

    private Node head;//头指针
    private Node rear;//记录尾指针
    private Node point;//临时指针
    private int length;//链表长度

    public TextLink() {
        head = new Node();
        rear = head;
        length = 0;
    }

    /**
     * 插入数据,从尾部添加
     */
    public void add(T elem) {
        point = new Node(elem);
        rear.next = point;
        rear = point;
        length++;
    }

    public void traverse() {
        point = head;
        if (head != null) {
            while (point.next != null) {
                System.out.print(" [" + point.next.data + "]");
                point = point.next;
            }
        }
    }

    /**
     * 获取链表长度
     */
    public int getLength() {
        return length;
    }

    /**
     * 将元素插入指定位置
     */
    public void insert(int position, T elem) {
        if (position >= 0 && position <= length) {
            point = movePoint(position);
            Node tmp = new Node(elem);
            tmp.next = point.next;
            point.next = tmp;
            length++;
        } else {
            throw new ArrayIndexOutOfBoundsException("insert position is" + position + ",max length is" + length);
        }
    }

    /**
     * 移除指定位置元素
     */
    public void remove(int position) {
        if (position >= 0 && position < length) {
            point = movePoint(position);
            Node tmp = point.next;
            point.next = tmp.next;
            length--;
        } else {
            throw new ArrayIndexOutOfBoundsException("insert position is" + position + ",max length is" + length);
        }
    }

    public void set(int position, T elem) {
        if (position >= 0 && position < length) {
            point = movePoint(position);
            Node tmp = point.next;
            tmp.data = elem;
        } else {
            throw new ArrayIndexOutOfBoundsException("insert position is" + position + ",max length is" + length);
        }
    }

    public T findByPosition(int position) {
        if (position >= 0 && position < length) {
            point = movePoint(position);
            return (T) point.next.data;
        } else {
            throw new ArrayIndexOutOfBoundsException("insert position is" + position + ",max length is" + length);
        }
    }

    /**
     * 通过内容查找下标
     */
    public int findByData(T elem) {
        int index = -1;
        point = head.next;
        while (point != null) {
            index++;
            if (point.data == elem) {
                break;
            }
            point = point.next;
        }
        return index;
    }

    /**
     * 查找position位置的元素
     */
    private Node movePoint(int position) {
        if (position >= 0 && position <= length) {
            point = head;
            while (point != null) {
                if (position == 0) {
                    break;
                }
                position--;
                point = point.next;
            }
        }
        return point;
    }
	   /**
     * 倒叙操作
     */
    public void reverse() {
        if (head == null || head.next == null) {
            return;
        }
        Node pre = null; // 前驱节点
        Node cur = null; // 当前节点
        Node next = null; // 后继节点

        // 把链表首节点变为尾节点
        cur = head.next;
        next = cur.next;
        cur.next = null;
        pre = cur;
        cur = next;
        // 使当前遍历到的节点 cur 指向前驱节点
        while (cur.next != null) {
            next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        cur.next = pre;
        head.next = cur;
    }
}

上面代码中 reverse() 为添加的倒序方法。假设当前已经遍历到了 cur 节点,由于它所有的前驱节点都已经完成了逆序操作,因此只需要使 cur.next = pre; 即可完成逆序操作。再此之前,为了记录当前节点的后继节点地址,需要用一个 next 的额外指针来保存后继节点信息。当前节点完成逆序后,通过后移指针来对后续节点用同样的方法进行逆序操作。

程序运行如下:

 public static void main(String[] args) {
        TextLink<String> link = new TextLink<>();
        link.add("1");
        link.add("2");
        link.add("3");
        link.add("4");
        link.add("5");
        link.add("6");
        link.add("7");
        link.traverse();

        link.reverse();
        System.out.println();
        link.traverse();
    }

输出如下:

 [1] [2] [3] [4] [5] [6] [7]
 [7] [6] [5] [4] [3] [2] [1]
  • 此方法性能分析

此方法只需要对链表进行一次遍历,因此时间复杂度为O(N)。 其中,N为链表的长度,但是需要额外的变量来保存当前节点的前驱节点和后驱节点因此控件复杂度为O(1)。

方法二:递归法
  • 根据递归的特性。先把 1-2-3-4-5-6-7 变为 1-7-6-5-4-3-2 然后把 1 放到最后。2 3 4 也是同样的操作,下面是代码:

  • 自定义了一个简单 Node类 并且创建链表

   private static class Node {
        private Object data;
        Node next;

        public Node() {
            data = null;
        }

        public Node(Object data) {
            this.data = data;
        }
    }
	// 创建链表
    private Node createLink() {
        // 构造链表
        Node head = new Node();
        Node temp = null;
        Node cur = head;

        for (int i = 0; i < 10; i++) {
            temp = new Node();
            temp.data = i;
            temp.next = null;
            cur.next = temp;
            cur = temp;
        }
        return head;
    }
  • 逆序带头节点的链表
	// 此方法对不带头的节点进行逆序
    private Node recursiveReverse(Node head) {
        if (head == null || head.next == null) {
            return head;
        } else {
            Node newHead = recursiveReverse(head.next);
            head.next.next = head;
            head.next = null;
            return newHead;
        }
    }
	// 对带头的节点进行逆序
    private void reverse2(Node head){
        if (head == null){
            return;
        }
        Node firstNode = head.next;
        Node newHead = recursiveReverse(firstNode);
        head.next = newHead;
    }

  • 调用
	@Test
    public void test() {
        // 新建链表
        Node head = createLink();
        // 逆序
        reverse2(head);
    }
  • 跟上边就地逆序的相比,虽然不需要保存前驱地址等,但是需要不断的方法压栈出栈,所以效率略低。并且需要申请新的存储空间。时间复杂度也是 O(N) N为链表长度。
方法三:插入法(推荐)

主要思路:先输出除了当前节点外的节点,然后输出当前节点。
1-2-3-4-5-6-7 输出为 2-3-4-5-6-7 然后输出 1。同理 2-3-4-5-6-7 输出 3-4-5-6-7 然后输出2.
与方法1比不需要保存前驱地址,也不需要递归。效率更高。

 	/**
     * 插入法 逆序
     */
    private void reverse3(Node head) {
        if (head == null || head.next == null) {
            return;
        }
        Node cur = null;
        Node next = null;
        // 当前从第二个开始 2
        cur = head.next.next;
        // 第一个节点变为尾
        head.next.next = null;
        while (cur != null) {
            next = cur.next;
            cur.next = head.next;
            head.next = cur;
            cur = next;
        }
    }
  • 15
    点赞
  • 87
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值