问题:一个单链表将其反转
单链表是一种常见的数据结构,对于链表中的每一个结点都包含两部分:数据域和指针域。例如,有一个链表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 具体步骤:
- 将head引用赋值给声明的当前引用 ListNode currentListNode = head; 声明当前引用节点的前置节点 ListNode prevListNode = null;将head引用节点的下一节点赋值给下一节点引用 ListNode nextListNode = head.getNext();
- 判断下一节点引用的值 即 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 具体步骤:
- 获取head节点的下一个节点 (ListNode nextListNode = head.getNext()😉, 若 nextListNode 为 null 说明到了临界条件,返回当前节点引用(即指向原链表最后一个节点的引用), 否则将 nextListNode 作为参数调用自身,一直向下传递。
- 每执行一次方法 , 此次方法中都会将下一节点的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();
}
}
结果:

参照:
- https://blog.youkuaiyun.com/lwkrsa/article/details/82015364
- https://blog.youkuaiyun.com/qq_32534441/article/details/91872381
1116

被折叠的 条评论
为什么被折叠?



