赞
踩
单向链表的反转Java实现如下 public class NodeList { public static class ListNode { int val; ListNode next; public ListNode(int val, ListNode next) { this.val = val; this.next = next; } } //递归修改当前节点的next节点,最终返回新的首节点 public static ListNode recursion(ListNode next) { if (next == null || next.next == null) { return next; } ListNode node = recursion(next.next); next.next.next = node; next.next = null; return node; } public static void main(String[] args) { ListNode node5 = new ListNode(5, null); ListNode node4 = new ListNode(4, node5); ListNode node3 = new ListNode(3, node4); ListNode node2 = new ListNode(2, node3); ListNode node1 = new ListNode(1, node2); ListNode node_1 = recursion(node1); System.out.println(node_1.val); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。