代码都是不含头结点的。
1、Java实现单链表的有效节点个数:
2、查找单链表中的倒数第k个节点
3.单链表反转
4.单链表逆序打印
----------------------------------------------------------------------------------------------------------------------
我把上面第四个常见面试题放在一个类里面,包含四个方法,并且在测试类中,调用方法进行测试
整体代码如下:
import javax.jws.WebParam;
import java.util.Stack;
public class LinkedList {
Node first;//指向链表的第一个节点
Node last;//指向链表的第二个节点
public Node getFirst() {
return first;
}
public void setFirst(Node first) {
this.first = first;
}
public Node getLast() {
return last;
}
public void setLast(Node last) {
this.last = last;
}
/**
* 首先判断链表是否为空
*
* @return
*/
public boolean isEmpty() {
return first == null;
}
/**
* 将当前链表的内容打印出来
*/
public void print() {
//指向当前链表的头结点
Node current = first;
//开始从头结点也就是第一个节点开始遍历
while (current != null) {
System.out.println("["+current.data + " " + current.name + " " + current.np+"]");
//下一个节点
current = current.next;
}
System.out.println();
}
/**
* 将指定节点插入到当前的链表中
*
* @param data 学生学号
* @param np 学生成绩
* @param name 学生姓名
*/
public void insert(int data, String name, int np) {
//创建一个新的节点对象
Node newNode = new Node(data, name, np);
if (this.isEmpty()) {
//如果链表为空,新节点作为第一个节点,同时也是最后一个节点
first = newNode;
last = newNode;
} else {
//不为空的话
last.next = newNode;
last = newNode;
}
}
/**
* 单链表的有效节点个数
*
* @param first
* @return
*/
public static int getLength(Node first) {
if (first == null) {
return 0;
}
int length = 0;
while (first != null) {
length++;
first = first.next;
}
return length;
}
/**
* 查找单链表中的倒数第k个节点
*/
public static Node findNode(Node first, int index) {
//查找第k个节点
if (first == null) {
return null;
}
int size = getLength(first);
if (index <= 0 || index > size){
return null;
}
for (int i = 0; i < size - index ; i++) {
first = first.next;
}
return first;
}
/**
* 单链表反转
*/
public static void reverseList(Node first){
//如果链表是空的或者只有一个,反转还是其本身
Node current = first;
Node before = null;
Node last = null;
if (current == null || current.next == null){
return ;
}
while (current != null) {
last = before;
before = current;
current = current.next;
before.next = last;
}
current = before;
while (current != null) {
System.out.println("["+current.data+" "+current.name+" "+
current.np+"]");
current = current.next;
}
System.out.println();
/**
* 单链表逆序打印
*/
public static void lastToBegin(Node first){
Stack<Node> stack = new Stack();
Node pop = null;
if (first == null ){
return ;
}
while (first != null) {
stack.push(first);
first = first.next;
}
//出栈
while (stack.size() > 0) {
System.out.println( pop = stack.pop());
}
}
}
/**
* 定义节点和数据
*/
class Node {
int data;
int np;
String name;
Node next;
public Node(int data, String name, int np) {
this.data = data;
this.np = np;
this.name = name;
this.next = null;
}
@Override
public String toString() {
return "Node{" +
"data=" + data +
", np=" + np +
", name='" + name + '\'' +
'}';
}
}
测试代码如下:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Tetst {
public static void main(String[] args) throws IOException {
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
int num;
String name;
int score;
System.out.println("请输入5位学生的数据:");
LinkedList list = new LinkedList();
for (int i = 0; i < 5; i++) {
System.out.println("请输入学号:");
num = Integer.parseInt(buf.readLine());
System.out.println("请输入姓名:");
name = buf.readLine();
System.out.println("请输入成绩:");
score = Integer.parseInt(buf.readLine());
list.insert(num,name,score);
System.out.println("-----------");
}
System.out.println("学生学号"+" "+"学生姓名"+" "+"学生成绩");
list.print();
int length = LinkedList.getLength(list.getFirst());
Node node = LinkedList.findNode(list.getFirst(), 2);
System.out.println(node);
System.out.println(length);
LinkedList.lastToBegin(list.getFirst());
System.out.println("原来链表的情况:");
list.print();
System.out.println("反转后的情况:");
LinkedList.reverseList(list.getFirst());
}
}
测试结果: