单链表 反转

本文详细介绍了一种单链表的数据结构,并通过具体实例演示了如何实现单链表的翻转。从创建节点开始,逐步构建链表,再通过迭代方式完成链表的翻转,最终打印验证结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package cm.com.datastructure.linkedlist;

/**
 * 实现单链表 翻转
 *
 * @author MemorysLiu
 * @version 1.0.0
 * @date 2019-07-31 15:14
 */
public class SinglyLinkedList {

    public static void main(String[] args) {

        Node node1 = new Node("1");
        Node node2 = new Node("2");
        Node node3 = new Node("3");
        Node node4 = new Node("4");
        Node node5 = new Node("5");
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;

        Node node = null;
        Node  current = node1;
        while (current != null){
            if (node == null){
                node = current;
                current = current.next;
                node.next = null;
            }else {
                Node temp = current;
                current = current.next;
                temp.next = node;
                node = temp;
            }
        }
        System.out.println(11);

    }


    private static class Node{
        String value;
        Node next;

        Node(String value){
            this.value = value;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值