From b3cd375f6128b4463b3dd68aa74e8318c73a0400 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Sat, 27 Feb 2021 22:59:21 +0800 Subject: [PATCH 1/2] leetcode 206 reverse List --- .../io/github/ehlxr/leetcode/ListNode.java | 56 ++++++++++++ .../io/github/ehlxr/leetcode/Solution206.java | 91 +++++++++++++++++++ .../java/io/github/ehlxr/lock/Locker.java | 43 +++++++++ 3 files changed, 190 insertions(+) create mode 100644 src/main/java/io/github/ehlxr/leetcode/ListNode.java create mode 100644 src/main/java/io/github/ehlxr/leetcode/Solution206.java create mode 100644 src/main/java/io/github/ehlxr/lock/Locker.java diff --git a/src/main/java/io/github/ehlxr/leetcode/ListNode.java b/src/main/java/io/github/ehlxr/leetcode/ListNode.java new file mode 100644 index 0000000..d8532a3 --- /dev/null +++ b/src/main/java/io/github/ehlxr/leetcode/ListNode.java @@ -0,0 +1,56 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2020 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.leetcode; + +/** + * 链表结构 + * + * @author ehlxr + * @since 2021-02-27 21:09. + */ +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 + + '}'; + } +} \ No newline at end of file diff --git a/src/main/java/io/github/ehlxr/leetcode/Solution206.java b/src/main/java/io/github/ehlxr/leetcode/Solution206.java new file mode 100644 index 0000000..b291c6a --- /dev/null +++ b/src/main/java/io/github/ehlxr/leetcode/Solution206.java @@ -0,0 +1,91 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2020 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.leetcode; + +/** + * 反转链表 + * 假设链表为 1 → 2 → 3 → null,我们想要把它改成 null ← 1 ← 2 ← 3 + * + * @author ehlxr + * @since 2021-02-27 21:08. + */ +public class Solution206 { + /* + 方法一:迭代 + https://leetcode-cn.com/problems/reverse-linked-list/solution/fan-zhuan-lian-biao-by-leetcode-solution-d1k2/ + */ + public static ListNode reverseList1(ListNode head) { + ListNode prev = null; + while (head != null) { + // 先存储当前节点的下一个节点 + ListNode next = head.next; + + // 将当前节点的下一个节点改为前一个节点 + head.next = prev; + + // 指针后移 + // 更改前一个节点为当前节点 + prev = head; + // 更改当前节点为下一个节点 + head = next; + } + + return prev; + } + + /* + 方法一:递归 + https://leetcode-cn.com/problems/reverse-linked-list/solution/shi-pin-jiang-jie-die-dai-he-di-gui-hen-hswxy/ + */ + public static ListNode reverseList2(ListNode head) { + // 递归终止条件 + if (head == null || head.next == null) { + return head; + } + + ListNode h = reverseList2(head.next); + + head.next.next = head; + // 防止链表循环 + head.next = null; + + return h; + } + + public static void main(String[] args) { + ListNode n1 = new ListNode(1); + ListNode n2 = new ListNode(2); + ListNode n3 = new ListNode(3); + ListNode n4 = new ListNode(4); + + n1.next = n2; + n2.next = n3; + n3.next = n4; + + System.out.println(n1); + + System.out.println(reverseList2(n1)); + } +} diff --git a/src/main/java/io/github/ehlxr/lock/Locker.java b/src/main/java/io/github/ehlxr/lock/Locker.java new file mode 100644 index 0000000..bacde9b --- /dev/null +++ b/src/main/java/io/github/ehlxr/lock/Locker.java @@ -0,0 +1,43 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2020 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.lock; + +import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; +import java.util.concurrent.locks.ReentrantLock; + +/** + * @author ehlxr + * @since 2021-02-15 18:11. + */ +public class Locker { + + + public static void main(String[] args) { + ReentrantLock lock = new ReentrantLock(); + + lock.tryLock(); + } + +} From fbf515f1bb0d15ef5af25a00495c84b6eb3dad49 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Sat, 27 Feb 2021 23:00:47 +0800 Subject: [PATCH 2/2] leetcode 206 reverse List --- src/main/java/io/github/ehlxr/lock/Locker.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/ehlxr/lock/Locker.java b/src/main/java/io/github/ehlxr/lock/Locker.java index bacde9b..3c3aec6 100644 --- a/src/main/java/io/github/ehlxr/lock/Locker.java +++ b/src/main/java/io/github/ehlxr/lock/Locker.java @@ -24,7 +24,6 @@ package io.github.ehlxr.lock; -import java.util.concurrent.locks.AbstractQueuedLongSynchronizer; import java.util.concurrent.locks.ReentrantLock; /** @@ -37,7 +36,12 @@ public class Locker { public static void main(String[] args) { ReentrantLock lock = new ReentrantLock(); - lock.tryLock(); + lock.lock(); // block until condition holds + try { + // ... method body + } finally { + lock.unlock(); + } } }