单链表的就地逆置算法
来源:互联网 发布:源码baiboku 编辑:程序博客网 时间:2024/06/03 17:26
public class Node {
int data;
Node next;
public Node(int data){
this.data = data;
}
}
package one;
import java.util.Scanner;
public class TestNode {
public static void main(String[] args) {
Node head = creat();
display(head);
head = invert(head);
display(head);
}
static Node creat(){
Scanner scanner = new Scanner(System.in);
int i= 1;
System.out.println("输入第"+i+"个参数");
int d = scanner.nextInt();
Node head;
head = new Node(d);
Node t=null,s=null;
s = head;
while(true){
i++;
System.out.println("输入第"+i+"个参数");
d = scanner.nextInt();
if(d==0)break;
if(d != 0){
t = new Node(d);
s.next = t;
s = t;
}
}
return head;
}
static void display(Node head){
System.out.println("链表:");
if(head == null){
System.out.println("null");
}System.out.println(head.data+"");
while(head.next !=null){
head = head.next;
System.out.println(head.data+"");
}
}
static Node invert(Node head){
Node t = null,s=null,v = null;
if(head == null || head.next == null){
System.out.println("逆置的单链表至少有2个结点");
return null;
}else{
t = head;
s = t.next;
while(s != null){
v = s.next;
s.next = t;
t=s;
s=v;
}
head.next = null;
head = t;
return head;
}
}
}
运行结果
输入第1个参数
1
输入第2个参数
2
输入第3个参数
3
输入第4个参数
4
输入第5个参数
0
链表:
1
2
3
4
链表:
4
3
2
1
- 单链表就地逆置的高效算法
- 实现单链表的就地逆置算法
- 单链表的就地逆置算法
- 单链表的就地逆置.
- 单链表的就地逆置
- 单链表的就地逆置
- 单链表的就地逆置
- 单链表的就地逆置
- 单链表的就地逆置
- 单链表就地逆置
- 单链表就地逆置
- 单链表就地逆置
- 单链表就地逆置
- 单链表就地逆置
- 单链表就地逆置
- 单链表就地逆置
- 单链表就地逆置
- 单链表就地逆置
- 在 Azure VM 上安装 LAMP Web 服务器
- 程序员必须要掌握WebView性能、体验分析与优化,开发才能做好
- 彻底理解ThreadLocal
- <mvc:annotation-driven>和<context:annotation-config>
- Dubbo管理平台和监控平台部署
- 单链表的就地逆置算法
- 存储过程mysql批量删除记录
- 亲测调试LCM
- mypulltorefresh_demo上拉刷新,下拉加载
- Jenkins入门系列之—— Jenkins安装与配置
- 深入剖析ThreadLocal
- centos7.3中RPM方式安装mysql5.7
- Git学习记录
- putty-连接服务器