数据结构-单链表作为栈使用,以及反转的思路

/**
 * @author zhangshiqiang on 2020/2/28.
 */
public class LinkedStack {
    private int size;
    private int curSize;
    private Node head;

    public LinkedStack(int size) {
        this.size = size;
        this.curSize = 0;
        head = new Node();
    }

    public boolean isFull() {
        return curSize == size;
    }

    public boolean isEmpty() {
        return curSize == 0;
    }

    public void push(Node node) {
        if (isFull()) {
            System.out.println("栈已满");
            return;
        }
        curSize++;
        Node cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.setNext(node);
    }

    public Node pop() {
        if (isEmpty()) {
            System.out.println("空栈");
            return null;
        }
        curSize--;
        Node cur = this.head;
        Node pre = null;
        while (cur.next != null) {
            pre = cur;
            cur = cur.next;
        }
        pre.setNext(null);
        return cur;
    }

    public LinkedStack revers2() {
            if (head.next == null) {
                return this;
            }
            Node pre = head.next;
            Node cur = pre.next;
            pre.next = null;
            Node tmp;
            head.next = null;
            while (cur != null) {
                tmp = cur.next;
                cur.next = pre;

                pre = cur;
                cur = tmp;
            }
            head.next = pre;
            return this;
    }

    public LinkedStack reverse() {
        Node cur = this.head;
        Node newNode = new Node();
        Node pre;
        // 0>1>2>3>4>5
        // 0>1
        // 0>2>1
        // 0>3>2>1
        // 0>4>3>2>1
        // 0>5>4>3>2>1
        while (cur.next != null) {
            // 第一次是 1
            // 第二次是 2
            pre = cur.next;
            // cur还在head节点,指向2
            // cur还在head节点,指向3
            cur.next = cur.next.next;

            if (newNode.next == null) {
                pre.next = null;
                newNode.next = pre;
            } else {
                pre.next = newNode.next;
                newNode.next = pre;
            }
        }
        setHead(newNode);
        return this;
    }

    public void setHead(Node head) {
        this.head = head;
    }

    private static class Node {
        private int i;
        private Node next;

        public Node() {
        }

        public Node(int i) {
            this.i = i;
        }

        public void setNext(Node next) {
            this.next = next;
        }

        @Override
        public String toString() {
            return "Node{" +
                    "i=" + i +
                    '}';
        }
    }

    public void list() {
        Node cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
            System.out.println(cur);
        }
    }

    @Override
    public String toString() {
        return "LinkedStack{" +
                "size=" + size +
                ", curSize=" + curSize +
                ", head=" + head +
                '}';
    }

    public static void main(String[] args) {
        LinkedStack linkedStack = new LinkedStack(5);
        linkedStack.push(new Node(1));
        linkedStack.push(new Node(2));
        linkedStack.push(new Node(3));
        linkedStack.push(new Node(4));
        linkedStack.push(new Node(5));
        linkedStack.push(new Node(6));

        linkedStack.reverse().list();
    }
}

 

<think>好的,用户让我介绍一下单链表反转操作思路。首先,我需要回忆一下单链表的结构。单链表每个节点包含数据和指向下一个节点的指针,反转的话需要改变每个节点的指针方向,让它们指向前一个节点而不是下一个。 用户可能是刚开始学习数据结构的新手,所以需要讲得清楚易懂。可能他们之前学过链表的基本操作,比如插入、删除,现在想进阶学习反转这样的操作。这时候我需要用简单的步骤来解释,避免使用复杂的术语。 首先,我应该考虑常见的反转方法,比如迭代法和递归法。迭代法更容易理解,适合新手,而递归法则可能稍微复杂一点,但代码简洁。用户可能更想知道迭代法的思路,所以先重点讲这个。 接下来,需要详细说明每一步的操作。比如,需要三个指针:前驱节点、当前节点和后继节点。在遍历链表的过程中,将当前节点的next指向前驱,然后三个指针依次向后移动。这个过程需要画个图或者用例子来说明,但因为是文字回复,可能需要用比喻或者步骤分解。 用户可能担心指针操作会出错,比如断链或者循环引用。这时候需要强调每一步的顺序,比如先保存下一个节点,再修改当前节点的指针,最后移动指针。这样避免丢失对后续节点的引用。 另外,边界条件也很重要。比如链表为空或者只有一个节点的情况,这时候反转后的链表应该和原来一样。需要提醒用户注意这些特殊情况,避免代码出错。 递归法可能作为补充,虽然效率可能不如迭代法,但代码更简洁。需要解释递归的终止条件和递归过程中的指针调整。可能用户对递归的理解还不够深入,所以需要简单说明递归的结构,以及如何通过递归从后往前反转指针。 最后,可以总结一下两种方法的优缺点,比如迭代法的时间复杂度是O(n),空间复杂度是O(1);递归法的时间复杂度相同,但空间复杂度是O(n)因为递归的开销。这样用户可以根据实际情况选择合适的方法。 用户可能在实际应用中遇到问题,比如链表有环或者需要反转部分链表,这时候可能需要更复杂的处理。不过当前的问题只是反转整个单链表,所以先集中讲解基础方法,确保用户掌握核心思路。 检查一下是否有遗漏的步骤,比如初始化前驱节点为null,然后在循环中逐步反转。确保每个步骤都解释清楚,避免用户混淆指针的移动顺序。可能需要用示例链表一步步演示反转过程,比如1->2->3->4变成4->3->2->1,这样更直观。 总之,思路要清晰,步骤分解详细,用例子辅助说明,并提醒注意边界条件和常见错误点。这样用户不仅能理解算法,还能自己实现时避免常见错误。</think>以下是单链表反转的详细实现思路,通过分步操作说明和数学表达式展示关键逻辑: --- ### 一、基础数据结构定义 设单链表节点结构为: ```math \text{Node} \triangleq \{\text{val}, \text{next}\} \quad \text{其中} \ \text{next} \in \text{Node} \cup \{\text{null}\} ``` 初始状态链表为: ```math n_1 \rightarrow n_2 \rightarrow \cdots \rightarrow n_k \rightarrow \text{null} ``` --- ### 二、迭代反转法(推荐方法) #### 1. 三指针操作原理 定义三个状态变量: ```math \begin{cases} prev: \text{已处理链表的头节点} \\ curr: \text{当前待处理节点} \\ next: \text{后续待处理节点} \end{cases} ``` #### 2. 操作步骤分解 | 步骤 | 操作表达式 | 图形化表示(以节点n_i为例) | |------|--------------------------|------------------------------| | 初始化 | \( prev \leftarrow \text{null} \)<br>\( curr \leftarrow head \) | null ← (curr) → n_2 → ... | | 步骤1 | \( next \leftarrow curr.next \) | null ← (curr) → (next) → ... | | 步骤2 | \( curr.next \leftarrow prev \) | null ← (prev) ← (curr) (next) → ... | | 步骤3 | \( prev \leftarrow curr \)<br>\( curr \leftarrow next \) | null ← prev ← curr (now next) → ... | #### 3. 终止条件 当满足: ```math curr = \text{null} \quad \Rightarrow \quad \text{新头节点} = prev ``` #### 4. 时间复杂度分析 ```math T(n) = O(n) \quad \text{(严格线性遍历)} ``` --- ### 三、递归反转法(补充方案) #### 1. 递归定义 ```math \text{reverse}(head) = \begin{cases} \text{null}, & \text{if } head = \text{null} \\ head, & \text{if } head.next = \text{null} \\ \text{reverse}(head.next) \rightarrow \text{调整指针} \end{cases} ``` #### 2. 关键操作步骤 1. 递归到末尾节点(新头节点) 2. 回溯时执行: ```math head.next.next = head \\ head.next = \text{null} ``` #### 3. 空间复杂度分析 ```math S(n) = O(n) \quad \text{(递归深度)} ``` --- ### 四、实例演算(迭代法) **初始状态**:1 → 2 → 3 → null | 轮次 | prev | curr | next | 链表状态 | |------|-------|------|------|--------------------------| | 初始 | null | 1 | - | null ← 1 → 2 → 3 → null | | 1 | 1 | 2 | 3 | null ← 1 ← 2 → 3 → null | | 2 | 2 | 3 | null | null ← 1 ← 2 ← 3 → null | | 3 | 3 | null | - | null ← 1 ← 2 ← 3 (新头) | --- ### 五、特殊场景处理 1. **空链表**:直接返回null 2. **单节点链表**:返回原头节点 3. **循环链表检测**:需先进行环检测(快慢指针法) --- ### 六、代码验证建议 1. 测试用例设计: ```text Case1: 输入 null → 输出 null Case2: 1 → null → 输出 1 → null Case3: 1 → 2 → 3 → 4 → null → 输出 4 → 3 → 2 → 1 → null ``` 2. 内存泄漏检查(C/C++): ```math \text{确保无节点丢失} \quad \sum_{i=1}^{n} \text{node}_i = \text{const} ``` --- 选择迭代法可获得最优空间效率,递归法代码更简洁但受深度限制。实际开发推荐迭代实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值