package cankao;
public class lianbiao_reverse{
static class Node{
private int value;
private Node next;
public Node(int value ){
this.value = value;
}
}
public static void main(String[] args){
Node head = new Node(1);
Node two = new Node(4);
Node three = new Node(6);
head.next = two;
two.next = three;
Node new_head = node_reverse(head);
while(new_head != null){
System.out.println(new_head.value);
new_head = new_head.next;
}
}
// 1->4->6->null
// 6->4->1->null
public static Node node_reverse(Node head){
Node pre= null;
while(head != null){
Node tmp = head.next ;
head.next = pre;
pre =head;
head = tmp;
}
return pre;
}
}
常见算法面试题-单链表反转
最新推荐文章于 2024-05-20 20:08:58 发布