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);
    
        }
    
    }
  • 相关阅读:
    剑指offer 39. 是否为平衡二叉树
    剑指offer 38. 二叉树的深度
    剑指offer 16. 合并两个有序链表
    剑指offer 15.链表反转
    剑指offer 58.对称的二叉树
    剑指offer 62. 二叉搜索树的第 k 个结点
    二叉树的前序和中序遍历的非递归实现
    将求模运算转换成与运算,加快运算速度
    Java中数字的格式化输出
    Java中的freopen()
  • 原文地址:https://www.cnblogs.com/chwy/p/5712542.html
Copyright © 2011-2022 走看看