diff --git a/README.md b/README.md index b65cef99b..eaee0a638 100644 --- a/README.md +++ b/README.md @@ -183,7 +183,7 @@ If you would like to have collaborator permissions on the repo to merge your own [0148 - Sort List](https://leetcode.com/problems/sort-list/) |
[✔️](c%2F0148-sort-list.c)
|
|
|
|
|
[✔️](java%2F0148-sort-list.java)
|
|
|
[✔️](python%2F0148-sort-list.py)
|
|
|
|
|
[0086 - Partition List](https://leetcode.com/problems/partition-list/) |
|
|
|
|
|
[✔️](java%2F0086-partition-list.java)
|
|
|
[✔️](python%2F0086-partition-list.py)
|
|
|
|
|
[0061 - Rotate List](https://leetcode.com/problems/rotate-list/) |
|
[✔️](cpp%2F0061-rotate-list.cpp)
|
|
|
|
[✔️](java%2F0061-rotate-list.java)
|
|
|
[✔️](python%2F0061-rotate-list.py)
|
|
|
|
|
-[0092 - Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) |
|
[✔️](cpp%2F0092-reverse-linked-list-ii.cpp)
|
|
|
|
|
[✔️](javascript%2F0092-reverse-linked-list-ii.js)
|
|
[✔️](python%2F0092-reverse-linked-list-ii.py)
|
|
|
|
|
+[0092 - Reverse Linked List II](https://leetcode.com/problems/reverse-linked-list-ii/) |
|
[✔️](cpp%2F0092-reverse-linked-list-ii.cpp)
|
|
|
|
[✔️](java%2F0092-reverse-linked-list-ll.java)
|
[✔️](javascript%2F0092-reverse-linked-list-ii.js)
|
|
[✔️](python%2F0092-reverse-linked-list-ii.py)
|
|
|
|
|
[0622 - Design Circular Queue](https://leetcode.com/problems/design-circular-queue/) |
|
|
|
|
[✔️](go%2F0622-design-circular-queue.go)
|
|
|
[✔️](kotlin%2F0622-design-circular-queue.kt)
|
[✔️](python%2F0622-design-circular-queue.py)
|
|
|
|
|
[0147 - Insertion Sort List](https://leetcode.com/problems/insertion-sort-list/) |
|
|
|
|
|
|
|
|
[✔️](python%2F0147-insertion-sort-list.py)
|
|
|
|
|
[0146 - LRU Cache](https://leetcode.com/problems/lru-cache/) |
[✔️](c%2F0146-lru-cache.c)
|
[✔️](cpp%2F0146-lru-cache.cpp)
|
[✔️](csharp%2F0146-lru-cache.cs)
|
|
[✔️](go%2F0146-lru-cache.go)
|
[✔️](java%2F0146-lru-cache.java)
|
[✔️](javascript%2F0146-lru-cache.js)
|
[✔️](kotlin%2F0146-lru-cache.kt)
|
[✔️](python%2F0146-lru-cache.py)
|
[✔️](ruby%2F0146-lru-cache.rb)
|
|
|
|
diff --git a/java/0092-reverse-linked-list-ll.java b/java/0092-reverse-linked-list-ll.java new file mode 100644 index 000000000..816f87321 --- /dev/null +++ b/java/0092-reverse-linked-list-ll.java @@ -0,0 +1,61 @@ +/** + * Definition for singly-linked list. + * 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; } + * } + */ +class Solution { + public ListNode reverseBetween(ListNode head, int left, int right) { + + if (head == null || head.next == null) { + return head; + } + + if (left > right || left == right) { + return head; + } + + ListNode l = head; + ListNode prevL = null; + ListNode r = head; + ListNode nextR = null; + int posL = 1; + int posR = 1; + + while (posL != left) { + prevL = l; + l = l.next; + posL++; + } + + while (posR != right) { + r = r.next; + posR++; + } + + nextR = r.next; + r.next = null; + ListNode node = reverseList(l); + node.next = nextR; + if (prevL == null) { + head = r; + } else { + prevL.next = r; + } + return head; + } + + public ListNode reverseList(ListNode l) { + if (l.next == null) { + return l; + } + ListNode newNode = reverseList(l.next); + newNode.next = l; + l.next = null; + return l; + } +}