package com.paulzhangcc.springtest;
/**
* @author paul
* @description
* @date 2020/1/16
*/
public class TestInvertLinkedList {
public static class Node {
public Node(String value) {
this.value = value;
}
public String value;
public Node next;
}
public static void main(String[] args) {
Node node1 = new Node("1");
Node node2 = new Node("2");
node1.next = node2;
Node node3 = new Node("3");
node2.next = node3;
Node node4 = new Node("4");
node3.next = node4;
Node node5 = new Node("5");
node4.next = node5;
print(node1);
print(fz(node1));
}
public static void print(Node node) {
while (node != null) {
System.out.print(node.value + " ");
node = node.next;
}
System.out.println();
}
public static Node fz(Node first) {
if (first == null || first.next == null) {
return first;
}
Node now = first;
Node p1 = first.next;
Node p2 = p1 == null ? null : p1.next;
first.next = null;
for (; ; ) {
p1.next = now;
if (p2 == null) {
break;
}
now = p1;
p1 = p2;
p2 = p1 == null ? null : p1.next;
}
return p1;
}
}
单链表反转
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- iOS 字符串反转、单链表反转 字符串反转 做法就是,设立两个begin和end哨兵,然后将这两个哨兵对应的值进行...