美文网首页
链表:查找倒数节点

链表:查找倒数节点

作者: 胶布小子 | 来源:发表于2020-02-06 20:57 被阅读0次

    本文为原创文章,转载请注明出处,谢谢你……
    喜欢java并发编程的请加群:736156823
    开始-->

    通过一次遍历找到倒数节点

    // 通过一次遍历找到倒数节点
    public class HeadToEnd {
    
        private HeadToEnd() {
    
        }
    
        public static final HeadToEnd create() {
            return new HeadToEnd();
        }
    
        public Node nthNodeFromEnd(Node head, int nth) {
            if (null == head) {
                throw new NullPointerException();
            }
            if (null == head.getNext()) {
                throw new IllegalArgumentException();
            }
            Node temp = head;
            Node find = null;
            for (int i = 1; i < nth; i++) {
                if (null == temp) {
                    break;
                } else {
                    temp = temp.getNext();
                }
            }
            for (; null != temp; ) {
                if (null == find) {
                    find = head;
                } else {
                    find = find.getNext();
                }
                temp = temp.getNext();
            }
            if (null != find) {
                return find;
            } else {
                // 答印警告
                return null;
            }
        }
    
        public static final void main(String[] args) {
            CreateList create = CreateList.create();
            Node head = create.buildNormal(8);
            Node temp = head;
            for (; null != temp; ) {
                System.out.println(temp);
                temp = temp.getNext();
            }
            HeadToEnd find = HeadToEnd.create();
            Node nth = find.nthNodeFromEnd(head, 13);
            System.out.println(nth);
        }
    
    }
    

    辅助类1:节点

    public class Node {
    
        private Node next;
        private int date;
    
        private Node() {
    
        }
    
        public static final Node create() {
            return new Node();
        }
    
        public void setNext(final Node next) {
            this.next = next;
        }
    
        public Node getNext() {
            return next;
        }
    
        public int getDate() {
            return date;
        }
    
        public void setDate(int date) {
            this.date = date;
        }
    
        @Override
        public String toString() {
            return "Node{" +
                    "date=" + date +
                    '}';
        }
    }
    

    辅助类2:链表创建类

    public class CreateList {
    
        private CreateList() {
    
        }
    
        public static final CreateList create() {
            return new CreateList();
        }
    
        public final Node buildNormal(int length) {
            return build(length).getHead();
        }
    
        private final HeadTail build(int length) {
            final Node head = Node.create();
            Node current = head;
            for (int i = 0; i < length; i++) {
                Node node = Node.create();
                node.setDate(i + 1);
                current.setNext(node);
                current = node;
            }
            final HeadTail ht = HeadTail.create(head, current);
            return ht;
        }
    
        public final Node buildLoop(int listLength, int loopLength) {
            if (listLength < 0) {
                throw new IllegalArgumentException();
            }
            if (listLength == 0) {
                return Node.create();
            }
            if (loopLength <= 0) {
                return buildNormal(listLength);
            }
            if (loopLength > listLength) {
                throw new IllegalArgumentException();
            }
            HeadTail ht = build(listLength);
            Node head = ht.getHead();
            Node tail = ht.getTail();
            if (loopLength == listLength) {
                tail.setNext(head.getNext());
            } else {
                Node temp = head;
                int dur = listLength - loopLength;
                for (int i = 0; i < dur; i++) {
                    temp = temp.getNext();
                }
                tail.setNext(temp.getNext());
            }
            return head;
        }
    
        private static final class HeadTail {
            private final Node head;
            private final Node tail;
    
            private HeadTail(final Node head, final Node tail) {
                this.head = head;
                this.tail = tail;
            }
    
            public static final HeadTail create(final Node head, final Node tail) {
                return new HeadTail(head, tail);
            }
    
            public Node getHead() {
                return head;
            }
    
            public Node getTail() {
                return tail;
            }
        }
    
    }
    

    运行截图

    image.png

    喜欢java并发编程的请加群:736156823
    有问题欢迎指正,这是新鲜出炉的
    结束-->
    本文为原创文章,转载请注明出处,谢谢你……

    相关文章

      网友评论

          本文标题:链表:查找倒数节点

          本文链接:https://www.haomeiwen.com/subject/uttpxhtx.html