From d4dd16ef960e44a6e0914ae4d28301173f493bbd Mon Sep 17 00:00:00 2001 From: ehlxr Date: Sun, 4 Dec 2022 22:58:59 +0800 Subject: [PATCH] update at 2022-12-04 22:58:59 by ehlxr --- .../algorithm/linkedlist/FindKthFromEnd.java | 16 ------ .../ehlxr/algorithm/linkedlist/Leecode21.java | 17 ------ .../ehlxr/algorithm/linkedlist/ListNode.java | 54 +++++++++++++++++++ .../algorithm/linkedlist/Palindrome.java | 24 --------- .../algorithm/linkedlist/ReverseKGroup.java | 24 --------- .../linkedlist/ReverseLinkedList.java | 52 ++++++------------ 6 files changed, 71 insertions(+), 116 deletions(-) create mode 100644 budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ListNode.java diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/FindKthFromEnd.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/FindKthFromEnd.java index d18ea6c..15672e7 100644 --- a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/FindKthFromEnd.java +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/FindKthFromEnd.java @@ -76,20 +76,4 @@ public class FindKthFromEnd { } - static class ListNode { - int val; - ListNode next; - - ListNode(int x) { - val = x; - } - - @Override - public String toString() { - return "ListNode{" + - "val=" + val + - ", next=" + next + - '}'; - } - } } diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/Leecode21.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/Leecode21.java index 49b0a38..0c58c94 100644 --- a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/Leecode21.java +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/Leecode21.java @@ -29,23 +29,6 @@ package io.github.ehlxr.algorithm.linkedlist; * @since 2022-04-15 08:41. */ public class Leecode21 { - public static class ListNode { - int val; - ListNode next; - - ListNode() { - } - - ListNode(int val) { - this.val = val; - } - - ListNode(int val, ListNode next) { - this.val = val; - this.next = next; - } - } - public static void main(String[] args) { ListNode list1 = new ListNode(1); ListNode list2 = new ListNode(2); diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ListNode.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ListNode.java new file mode 100644 index 0000000..0765b95 --- /dev/null +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ListNode.java @@ -0,0 +1,54 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2022 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.algorithm.linkedlist; + +/** + * @author ehlxr + * @since 2022-12-04 22:25. + */ +public class ListNode { + int val; + ListNode next; + + ListNode() { + } + + ListNode(int val) { + this.val = val; + } + + ListNode(int val, ListNode next) { + this.val = val; + this.next = next; + } + + @Override + public String toString() { + return "ListNode{" + + "val=" + val + + ", next=" + next + + '}'; + } +} diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/Palindrome.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/Palindrome.java index 6fd4307..2a0fa6e 100644 --- a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/Palindrome.java +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/Palindrome.java @@ -88,28 +88,4 @@ public class Palindrome { return true; } - private static class ListNode { - int val; - ListNode next; - - ListNode() { - } - - ListNode(int val) { - this.val = val; - } - - ListNode(int val, ListNode next) { - this.val = val; - this.next = next; - } - - @Override - public String toString() { - return "ListNode{" + - "val=" + val + - ", next=" + next + - '}'; - } - } } diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseKGroup.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseKGroup.java index 0367810..d75d044 100644 --- a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseKGroup.java +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseKGroup.java @@ -65,28 +65,4 @@ public class ReverseKGroup { return pre; } - public static class ListNode { - int val; - ListNode next; - - ListNode() { - } - - ListNode(int val) { - this.val = val; - } - - ListNode(int val, ListNode next) { - this.val = val; - this.next = next; - } - - @Override - public String toString() { - return "ListNode{" + - "val=" + val + - ", next=" + next + - '}'; - } - } } diff --git a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseLinkedList.java b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseLinkedList.java index 77cacbf..c89acf9 100644 --- a/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseLinkedList.java +++ b/budd-common/src/main/java/io/github/ehlxr/algorithm/linkedlist/ReverseLinkedList.java @@ -32,23 +32,23 @@ package io.github.ehlxr.algorithm.linkedlist; */ public class ReverseLinkedList { public static void main(String[] args) { - Node node5 = new Node(5, null); - Node node4 = new Node(4, node5); - Node node3 = new Node(3, node4); - Node node2 = new Node(2, node3); - Node head = new Node(1, node2); + ListNode ListNode5 = new ListNode(5, null); + ListNode ListNode4 = new ListNode(4, ListNode5); + ListNode ListNode3 = new ListNode(3, ListNode4); + ListNode ListNode2 = new ListNode(2, ListNode3); + ListNode head = new ListNode(1, ListNode2); - // Node reverse = reverse(root); + // ListNode reverse = reverse(root); // reverse.print(); - // System.out.println(reverseToN(head, node3)); + // System.out.println(reverseToN(head, ListNode3)); // System.out.println(reverseToN(head, 2)); System.out.println(reverseFm2N(head, 2, 4)); } - public static Node reverseToN(Node root, Node n) { - Node pre = null, cur = root, tmp = root; + public static ListNode reverseToN(ListNode root, ListNode n) { + ListNode pre = null, cur = root, tmp = root; while (cur != n) { tmp = cur.next; @@ -61,21 +61,21 @@ public class ReverseLinkedList { } - static Node next = null; + static ListNode next = null; - public static Node reverseToN(Node root, int n) { + public static ListNode reverseToN(ListNode root, int n) { if (n == 1) { next = root.next; return root; } - Node h = reverseToN(root.next, n - 1); + ListNode h = reverseToN(root.next, n - 1); root.next.next = root; root.next = next; return h; } - public static Node reverseFm2N(Node root, int m, int n) { + public static ListNode reverseFm2N(ListNode root, int m, int n) { if (m == 1) { return reverseToN(root, n); } @@ -83,12 +83,12 @@ public class ReverseLinkedList { return root; } - public static Node reverse(Node root) { + public static ListNode reverse(ListNode root) { - Node pre = null; - Node cur = root; + ListNode pre = null; + ListNode cur = root; while (cur != null) { - Node tmp = cur.next; + ListNode tmp = cur.next; cur.next = pre; pre = cur; @@ -99,21 +99,3 @@ public class ReverseLinkedList { return pre; } } - -class Node { - public Integer value; - public Node next; - - public Node(Integer value, Node next) { - this.next = next; - this.value = value; - } - - @Override - public String toString() { - return "Node{" + - "value=" + value + - ", next=" + next + - '}'; - } -}