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);
    
        }
    
    }
  • 相关阅读:
    .NET备份博客园随笔分类文章
    部署到IIS报错:HTTP错误500.19,错误代码0x80070021
    部署到IIS报错:HTTP错误500.19,错误代码0x800700d
    css布局
    将Windows系统编译的.NET Core程序发布到Ubuntu系统
    在Ubuntu搭建.NET Core环境
    在Windows系统搭建.NET Core环境并创建运行ASP.NET网站
    2017-2018-2 1723《程序设计与数据结构》助教总结
    2017-2018-2 1723《程序设计与数据结构》实验四 & 实验五 & 课程总结 总结
    2017-2018-2 1723《程序设计与数据结构》第十一周作业 & 实验三 & (总体)第三周结对编程 总结
  • 原文地址:https://www.cnblogs.com/chwy/p/5712542.html
Copyright © 2011-2022 走看看