反转单链表

作者: codezwc | 来源:发表于2018-05-12 18:22 被阅读1次

题目:反转单链表。

public class 反转单向链表 {

    public static class Node {
        int data;
        Node next;

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

    public static Node reverList(Node head) {
        if (head == null) {
            return head;
        }
        Node temp = null;
        Node curr = null;
        while (head != null) {
            curr = head.next;
            head.next = temp;
            temp = head;
            head = curr;
        }
        return temp;
    }

    public static void main(String[] args) {
        Node A1 = new Node(1);
        Node A2 = new Node(2);
        Node A3 = new Node(3);
        Node A4 = new Node(4);
        Node A5 = new Node(5);
        A1.next = A2;
        A2.next = A3;
        A3.next = A4;
        A4.next = A5;
        Node head = reverList(A1);

        while (head != null) {
            System.out.println(head.data);
            head = head.next;
        }
    }
}

相关文章

  • Algorithm小白入门 -- 单链表

    单链表递归反转链表k个一组反转链表回文链表 1. 递归反转链表 单链表节点的结构如下: 1.1 递归反转整个单链表...

  • 单链表反转

    单链表 单链表反转 递归方法

  • Java、Python3 实战 LeetCode 高频面试之单链

    单链表反转 单链表反转这道题可谓是链表里面的高频问题了,差不多可以说只要被问到链表,就会问单链表反转。 今天我们就...

  • 链表简单算法相关练习

    单链表反转: 给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。 迭代方式实现: 复杂度分析: 时...

  • 5个链表的常见操作

    链表 链表反转 LeetCode206:给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。 环路检...

  • 反转链表

    给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

  • 【教3妹学算法】2道链表类题目

    题目1:反转链表 给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。 示例 1: 输入:head ...

  • js+链表

    链表结构 删除链表某节点 遍历 反转单链表

  • 反转单链表

    题目:反转单链表。

  • 单链表反转

    单链表反转 单链表初始化 输出 反转 释放 实现代码 尚未实现 元素插入 元素删除

网友评论

    本文标题:反转单链表

    本文链接:https://www.haomeiwen.com/subject/zgehdftx.html