单向链表的反转JAVA实现(循环)(咋个办呢 zgbn)
上一篇文章介绍了“单向链表反转”通过“递归”方式的实现,这边文章介绍一下如何通过“循环”实现。其实逻辑都是一样的。
相关文章:单向连表的反转JAVA实现(递归)(咋个办呢 zgbn)
分析过程
实现任何需求,我强烈建议一定要好先做好分析过程,然后写起代码就思路清晰可见。避免陷入思维混乱。
1、单向链表结构
2、从前到后遍历,第一次反转节点对象的指向,这里需要注意,橘色
是一个用于临时维系链表关系的。当节点2的指向反转后,实际上节点2、节点3之间,丢失指向关系。所以需要外部节点进行维护。
3、第二次反转节点2、节点3,此时节点3、节点4之间失去指向关系,需要通过橘色
节点、节点4维系指向关系。往下一次类推。
4、一直到最后一次反转节点4、节点5完成。
代码
这里贴出了所有代码,并且在上一个文章中代码来说,有些小的调整。
1、单向连表的节点类
public class Item {
private int value;
private Item next;
public Item() {
}
public Item(int value) {
this.value = value;
}
public Item(Item value) {
this.value = value.getValue();
this.next = (value.getNext());
}
public int getValue() {
return value;
}
public Item setValue(int value) {
this.value = value;
return this;
}
public Item getNext() {
return next;
}
public Item setNext(Item next) {
this.next = next;
return this;
}
@Override
public String toString() {
return String.valueOf(this.value);
}
}
2、单向连表类
public class ItemList {
/**
* 起始节点对象
*/
private Item item;
/**
* 最后节点对象
*/
private Item endItem;
/**
* 第一个节点坐标,最后节点坐标
*/
private int start, end;
/**
* 构造函数,构建链表实体
*
* @param values 链表存储数字
*/
public ItemList(int... values) {
int len = values.length;
Item item = null;
for (int i = 0; i < len; i++) {
if (i == 0) {
item = this.item = this.endItem = new Item(values[i]);
} else {
item = this.endItem = item.setNext(new Item(values[i])).getNext();
}
}
}
@Override
public String toString() {
StringBuffer str = new StringBuffer();
Item item = this.item;
if (item != null) {
str.append(item.getValue()).append(",");
while (item.getNext() != null) {
item = item.getNext();
str.append(item.getValue()).append(",");
}
}
return str.toString();
}
/**
* 执行链表反转动作,循环实现
*/
public void inversionCycle() {
if (this.item != null && this.item.getNext() != null) {
this.inversionCycle(this.item);
this.item.setNext(null);
Item tm = this.endItem;
this.endItem = this.item;
this.item = tm;
}
}
/**
* 执行链表反转动作,递归实现
*/
public void inversionRecursion() {
if (this.item != null && this.item.getNext() != null) {
this.inversionRecursion(this.item, this.item.getNext());
this.item.setNext(null);
Item tm = this.endItem;
this.endItem = this.item;
this.item = tm;
}
}
/**
* 链表反转递归实现
*
* @param thisItem 当前节点
* @param nextItem 当前节点的下一个节点
* @return 当前节点
*/
private Item inversionRecursion(Item thisItem, Item nextItem) {
if (nextItem.getNext() != null) {
nextItem = inversionRecursion(nextItem, nextItem.getNext());
}
nextItem.setNext(thisItem.setNext(null));
return thisItem;
}
/**
* 链表反转循环实现。
*
* @param thisItem 当前节点
* @return
*/
private Item inversionCycle(Item thisItem) {
Item tm = new Item(thisItem), nextItem = null;
while (tm.getNext() != null) {
nextItem = tm.getNext();
tm.setNext(nextItem.getNext()).setValue(nextItem.getValue());
thisItem = nextItem.setNext(thisItem);
}
return thisItem;
}
}
3、执行和结果
public class LinkedListInversionDemo {
private static ItemList ilist = new ItemList(1, 2, 3, 4, 5, 6, 7, 8, 9);
public static void main(String[] args) {
System.out.println(String.format("单向链表数据:\t%s", ilist));
ilist.inversionCycle();// 执行反转动作
System.out.println(String.format("单向链表反转后的数据:\t%s", ilist));
}
}
4、执行结果
单向链表数据: 1,2,3,4,5,6,7,8,9,
单向链表反转后的数据: 9,8,7,6,5,4,3,2,1,