链表常见题型(java版)

直接上干货。。。。。

链表常见题型:

  1. 找到单链表的倒数第k个节点。
  2. 删除单链表中的某个结点(O(1))。
  3. 反转链表。
  4. 两个链表的第一个公共结点。
  5. 有环链表返回环路的开头节点(及判断是否有环)。
  6. 合并两个排序的链表。
  7. 删除链表中重复的结点。

先给出链表的定义:


    /**
     * 单链表定义
     */
    public static class Node<E>{
   
        private E element;//节点保存的元素
        private Node<E> next;//指向下一个节点的引用

        public Node(){}
        public Node(E element){
            this.element = element;
        }
        public Node(E element,Node<E> next){
            this.element = element;
            this.next = next;
        }

        public E getElement(){
            return element;
        }
        public Node<E> getNext(){
            return next;
        }
        public void setElement(E element){
            this.element = element;
        }
        public void setNext(Node<E> next){
            this.next = next;
        }
    }

关于鲁棒性:

鲁棒性也称为健壮性,是指程序能够判断输入是否合乎规范要求,并对不合要求的输入予以合理的处理。容错性是鲁棒性的一个重要体现。提高代码鲁棒性的有效途径是进行防御性编程,防御性编程是一种编程习惯,是指预见在什么地方可能会出现问题,并为这些可能出现问题制定处理方式。在面试时,最常用也是最有效的防御性编程是在函数入口添加代码以验证用户输入是否符合要求。

1. 找到单链表的倒数第k个节点。

这里假设最后一个节点为倒数第一个。思想:快慢指针。实现代码如下:


    public static Node<Integer> solution(Node<Integer> head,int k){
        // 判断k是否非法
        if(k < 1){
  return null;}
        // 判断是否为空
        if(head == null) return null;
        // 快指针
        Node<Integer> nAhead = head;
        // 慢指针
        Node<Integer> nBehind = head;
        // 快指针先走k-1步
        for(int i = 0;i<k-1;i++){
            // 判断是否存在倒数第k个节点
            if(nAhead.getNext() != null)
                nAhead = nAhead.getNext();
            else
                return null;
        }
        
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环链表是解决约瑟夫问的一种常见方法。下面是一个使用Java循环链表解决约瑟夫问的示例: ```java class Node { int data; Node next; public Node(int data) { this.data = data; } } class CircularLinkedList { private Node head; private Node tail; public void add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; tail = newNode; tail.next = head; } else { tail.next = newNode; tail = newNode; tail.next = head; } } public void remove(Node node) { if (head == null) { return; } if (head == node) { head = head.next; tail.next = head; } else { Node current = head; while (current.next != node) { current = current.next; } current.next = node.next; if (node == tail) { tail = current; tail.next = head; } } } public void josephus(int k) { Node current = head; while (current.next != current) { for (int i = 1; i < k; i++) { current = current.next; } System.out.print(current.data + " "); remove(current); current = current.next; } System.out.println(current.data); } } public class Main { public static void main(String[] args) { CircularLinkedList list = new CircularLinkedList(); int n = 7; // 总人数 int k = 3; // 报数为3的人出列 for (int i = 1; i <= n; i++) { list.add(i); } System.out.println("出列顺序:"); list.josephus(k); } } ``` 这段代码中,我们首先定义了一个`Node`类来表示循环链表的节点,然后定义了`CircularLinkedList`类来实现循环链表的操作。在`josephus`方法中,我们使用循环链表模拟约瑟夫问的解决过程,每次找到第k个节点并移除,直到只剩下一个节点为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值