• 在单链表和双链表中删除倒数第K个节点


    package chapter_2_listproblem;
    
    public class Problem_02_RemoveLastKthNode {
    
        public static class Node {
            public int value;
            public Node next;
    
            public Node(int data) {
                this.value = data;
            }
        }
    
        public static Node removeLastKthNode(Node head, int lastKth) {
            if (head == null || lastKth < 1) {
                return head;
            }
            Node cur = head;
            while (cur != null) {
                lastKth--;
                cur = cur.next;
            }
            if (lastKth == 0) {
                head = head.next;
            }
            if (lastKth < 0) {
                cur = head;
                while (++lastKth != 0) {
                    cur = cur.next;
                }
                cur.next = cur.next.next;
            }
            return head;
        }
    
        public static class DoubleNode {
            public int value;
            public DoubleNode last;
            public DoubleNode next;
    
            public DoubleNode(int data) {
                this.value = data;
            }
        }
    
        public static DoubleNode removeLastKthNode(DoubleNode head, int lastKth) {
            if (head == null || lastKth < 1) {
                return head;
            }
            DoubleNode cur = head;
            while (cur != null) {
                lastKth--;
                cur = cur.next;
            }
            if (lastKth == 0) {
                head = head.next;
                head.last = null;
            }
            if (lastKth < 0) {
                cur = head;
                while (++lastKth != 0) {
                    cur = cur.next;
                }
                DoubleNode newNext = cur.next.next;
                cur.next = newNext;
                if (newNext != null) {
                    newNext.last = cur;
                }
            }
            return head;
        }
    
        public static void printLinkedList(Node head) {
            System.out.print("Linked List: ");
            while (head != null) {
                System.out.print(head.value + " ");
                head = head.next;
            }
            System.out.println();
        }
    
        public static void printDoubleLinkedList(DoubleNode head) {
            System.out.print("Double Linked List: ");
            DoubleNode end = null;
            while (head != null) {
                System.out.print(head.value + " ");
                end = head;
                head = head.next;
            }
            System.out.print("| ");
            while (end != null) {
                System.out.print(end.value + " ");
                end = end.last;
            }
            System.out.println();
        }
    
        public static void main(String[] args) {
            Node head1 = new Node(1);
            head1.next = new Node(2);
            head1.next.next = new Node(3);
            head1.next.next.next = new Node(4);
            head1.next.next.next.next = new Node(5);
            head1.next.next.next.next.next = new Node(6);
            printLinkedList(head1);
            head1 = removeLastKthNode(head1, 3);
            // head1 = removeLastKthNode(head1, 6);
            // head1 = removeLastKthNode(head1, 7);
            printLinkedList(head1);
    
            DoubleNode head2 = new DoubleNode(1);
            head2.next = new DoubleNode(2);
            head2.next.last = head2;
            head2.next.next = new DoubleNode(3);
            head2.next.next.last = head2.next;
            head2.next.next.next = new DoubleNode(4);
            head2.next.next.next.last = head2.next.next;
            head2.next.next.next.next = new DoubleNode(5);
            head2.next.next.next.next.last = head2.next.next.next;
            head2.next.next.next.next.next = new DoubleNode(6);
            head2.next.next.next.next.next.last = head2.next.next.next.next;
            printDoubleLinkedList(head2);
            head2 = removeLastKthNode(head2, 3);
            // head2 = removeLastKthNode(head2, 6);
            // head2 = removeLastKthNode(head2, 7);
            printDoubleLinkedList(head2);
    
        }
    
    }
  • 相关阅读:
    Springboot打包成WAR包独立布署后找不到静态js文件
    layui实现数据分页功能(ajax异步)
    layer.prompt(options, yes)
    layer回调函数
    Html中的position:absolute的意思
    SQL基础-DML
    mysql的pager命令
    由于rngd进程导致的tomcat 启动慢
    elasticsearch安装
    zookeeper的observer模式
  • 原文地址:https://www.cnblogs.com/chwy/p/5712542.html
Copyright © 2020-2023  润新知