java实现单链表的反转

问题:一个单链表将其反转

     单链表是一种常见的数据结构,对于链表中的每一个结点都包含两部分:数据域和指针域。例如,有一个链表1—>2—>3—>4,反转后为4—>3—>2—>1。

解决:

1. 遍历反转法
2. 递归反转法

定义链表结点的实体类

代码:

public class ListNode {
	private Object data;//单链表结点的数据域
	private ListNode next;//单链表结点的指针域
	
	public ListNode(Object data){
		this.data = data;
	}
	public ListNode(Object data,ListNode next){
		this.data = data;
		this.next = next;
	}
	
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
	public ListNode getNext() {
		return next;
	}
	public void setNext(ListNode next) {
		this.next = next;
	}	
}

1.遍历反转法

1.1 具体步骤:

  1. 将head引用赋值给声明的当前引用 ListNode currentListNode = head; 声明当前引用节点的前置节点 ListNode prevListNode = null;将head引用节点的下一节点赋值给下一节点引用 ListNode nextListNode = head.getNext();
  2. 判断下一节点引用的值 即 nextListNode 指向的值是否为null,
    2.1)如果 nextListNode 指向的值不为null, 则将当前节点引用的next值设置成 prevListNode         (currentListNode.setNext(prevListNode)😉,然后,前置节点引用向后移动一个位置
            (ListNode prevListNode = currentListNode;) 当前节点向后移动一个位置(ListNode currentListNode =          nextListNode;),下一节点引用向后移动一个位置(ListNode nextListNode = nextListNode.getNext()😉
    2.2)如果 nextListNode 指向的值为null ,则将当前节点引用的next值设置成 prevListNode          (currentListNode.setNext(prevListNode)😉,返回当前引用节点 currentListNode。

1.2 图形展示:
在这里插入图片描述

1.3 代码:

	//遍历反转法
	public static Node reverseListNode1(Node head){
        if (head == null) {
            return null;
        }

        ListNode prevListNode = null;
        ListNode currentListNode = head;
        ListNode nextListNode = head.getNext();

        while (true){
            if (nextListNode != null) {
                currentListNode.setNext(prevListNode);
                prevListNode = currentListNode;
                currentListNode = nextListNode;
                nextListNode = nextListNode.getNext();
            }else {
                currentListNode.setNext(prevListNode);
                return currentListNode;
            }
        }
	}

2.递归反转法

2.1 具体步骤:

  1. 获取head节点的下一个节点 (ListNode nextListNode = head.getNext()😉, 若 nextListNode 为 null 说明到了临界条件,返回当前节点引用(即指向原链表最后一个节点的引用), 否则将 nextListNode 作为参数调用自身,一直向下传递。
  2. 每执行一次方法 , 此次方法中都会将下一节点的next设置为当前节点 (nextListNode.setNext(head)😉 但是这个设置操作是先调用的方法后执行此操作。

注意: 原链表的head引用一直指向原链表的头节点,所以每次执行方法时 都要执行一下 head.setNext(null); 若不进行这个设置,方法执行完后,反转后的链表的最后两个节点会互相指向对方。

2.2 图形展示:

在这里插入图片描述

2.3 代码:

	//递归法反转链表
	public static Node reverseListNode2(Node head){
        if (head == null) {
            return null;
        }

        ListNode nextListNode = head.getNext();
        if (nextListNode == null) {
            return head;
        }

        ListNode newHead = reverseList2(nextListNode);
        nextListNode.setNext(head);
        head.setNext(null);
        return newHead;
	}

测试代码:

	public static void main(String[] args) {
		
		ListNode node1 = new ListNode(1);
		ListNode node2 = new ListNode(2);
		ListNode node3 = new ListNode(3);
		ListNode node4 = new ListNode(4);
		
		node1.setNext(node2);
		node2.setNext(node3);
		node3.setNext(node4);
		
		ListNode h = node1;
		while(h!=null){
			System.out.print(h.getData()+" ");
			h = h.getNext();
		}
		System.out.println();
		System.out.println("=============================");
		ListNode head = reverseListNode1(node1);
		while(head!=null){
			System.out.print(head.getData()+" ");
			head = head.getNext();
		}
		
	}

结果:
在这里插入图片描述
参照:

  1. https://blog.youkuaiyun.com/lwkrsa/article/details/82015364
  2. https://blog.youkuaiyun.com/qq_32534441/article/details/91872381
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值