单链表的常见面试题

单链表的常见面试题有如下:

  1. 求单链表中有效节点的个数
  2. 查找单链表中的倒数第k个结点 【新浪面试题】
  3. 单链表的反转【腾讯面试题】
  4. 从尾到头打印单链表 【百度,要求方式1:反向遍历 。 方式2:Stack栈】
  5. 合并两个有序的单链表,合并之后的链表依然有序【课后练习.】
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.reverseList();
        linkedList.show();*/

        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;
        }

    }

    //根据no的顺序插入链表
    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;

    }

    //从尾到头打印单链表 【百度,要求方式1:反向遍历 。 方式2:Stack栈】
    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;  //用于保存temp的下一个节点
        while (temp!=null){
            temp2 = temp.next;//防止temp.next节点丢失
            temp.next = newHead.next;//插入新的链表的头部
            newHead.next=temp;
            temp=temp2;//定位到原来的temp.next节点
        }
        head.next=newHead.next; //更换原始头节点
    }

    //查找单链表中的倒数第k个结点 【新浪面试题】
    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 + '\'' +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值