单链表的常见面试题有如下:
- 求单链表中有效节点的个数
- 查找单链表中的倒数第k个结点 【新浪面试题】
- 单链表的反转【腾讯面试题】
- 从尾到头打印单链表 【百度,要求方式1:反向遍历 。 方式2:Stack栈】
- 合并两个有序的单链表,合并之后的链表依然有序【课后练习.】
import com.sun.glass.ui.Size;
import java.util.Stack;
public class SingleLinkedListTest {
public static void main(String[] args) {
Node node1 = new Node(1, "1号节点");
Node node3 = new Node(3, "3号节点");
Node node5 = new Node(5, "5号节点");
Node node7 = new Node(7, "7号节点");
LinkedList linkedList = new LinkedList();
linkedList.add(node1);
linkedList.add(node3);
linkedList.add(node5);
linkedList.add(node7);
linkedList.show();
System.out.println("\n==========================");
System.out.println("链表的有效个数为:"+ linkedList.getLinkedSize());
System.out.println("\n=====查找单链表中的倒数第k个结点======");
System.out.println(linkedList.findLastIndexNode(3));
System.out.println("\n==========反向打印============");
linkedList.reversePrintf();
System.out.println("\n==========合并两个有序的单链表============");
Node node2 = new Node(2, "第二个链表2号节点");
Node node4 = new Node(4, "第二个链表4号节点");
Node node6 = new Node(6, "第二个链表6号节点");
Node node8 = new Node(8, "第二个链表8号节点");
LinkedList linkedList2 = new LinkedList();
linkedList2.add(node2);
linkedList2.add(node4);
linkedList2.add(node6);
linkedList2.add(node8);
linkedList.listAddListByOrder(linkedList2.head);
linkedList.show();
}
}
class LinkedList{
public Node head = new Node(0, "头节点");
public void listAddListByOrder(Node head2){
Node temp = head2.next;
if (temp==null){
return;
}
Node tempNext =null;
while (temp!=null) {
tempNext = temp.next;
addByOrder(temp);
temp = tempNext;
}
}
public void addByOrder(Node node){
Node temp = head;
while (temp.next!=null){
if (temp.next.no>=node.no){
break;
}
temp = temp.next;
}
node.next=temp.next;
temp.next=node;
}
public void reversePrintf(){
Stack<Node> stack = new Stack<>();
Node temp = head.next;
while (temp!=null){
stack.push(temp);
temp = temp.next;
}
while (stack.size()>0){
System.out.println(stack.pop());
}
}
public void reverseList(){
if (head.next==null||head.next.next==null){
return;
}
Node newHead = new Node(0, null);
Node temp = head.next;
Node temp2 = null;
while (temp!=null){
temp2 = temp.next;
temp.next = newHead.next;
newHead.next=temp;
temp=temp2;
}
head.next=newHead.next;
}
public Node findLastIndexNode(int lastIndex){
if (head.next==null){
return null;
}
int size = getLinkedSize();
Node temp = head.next;
for (int i = 0; i <size-lastIndex ; i++) {
temp = temp.next;
}
return temp;
}
public int getLinkedSize(){
int size = 0;
Node temp = head.next;
while (temp!=null){
size++;
temp=temp.next;
}
return size;
}
public void add(Node node) {
Node temp = head;
while (temp.next!=null){
temp = temp.next;
}
temp.next = node;
}
public void show() {
if (head.next == null) {
System.out.println("链表为空");
return;
}
Node temp = head;
while (temp.next != null) {
System.out.println(temp.next);
temp = temp.next;
}
}
}
class Node {
public int no;
public String name;
public Node next;
public Node(int no, String name) {
this.no = no;
this.name = name;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
}