zoukankan      html  css  js  c++  java
  • 在单链表和双链表中删除倒数第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);
    
        }
    
    }
  • 相关阅读:
    谷歌SEO和百度SEO的区别
    如何在本地搭建网站(图文教程)
    企业级Web服务器安全主动防御措施
    如何在Ubuntu下搭建tftp服务器
    十条服务器端优化Web性能的技巧
    APP开发者如何从应用程序中赚钱?
    5个让你的404页面变的更加实用的技巧
    修改linux的MAC地址
    剪切文件或目录命令
    shell内置命令和外部命令的区别
  • 原文地址:https://www.cnblogs.com/chwy/p/5712542.html
Copyright © 2011-2022 走看看