mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-05 21:57:38 +00:00
docs: add new problem documentation pages and update redirects
This commit is contained in:
1 parent
ea44a99c40
commit
f1a952f2bd
7 files changed
+358
No files matched your search
@@ -0,0 +1,64 @@
|
||||
---
|
||||
title: '19. Remove Nth Node From End of List'
|
||||
description: Given the head of a linked list, remove the n^th node from the end of the list and return its head
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Linked List", slug: "linked-list" }
|
||||
- { name: "Two Pointers", slug: "two-pointers" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/80"
|
||||
sidebar:
|
||||
label: 'Remove Nth Node From End of List'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
::::warning
|
||||
Could you do this in one pass?
|
||||
::::
|
||||
|
||||
### Example 1:
|
||||
- Input: `head = [1,2,3,4,5], n = 2`
|
||||
- Output: `[1,2,3,5]`
|
||||
|
||||
### Example 2:
|
||||
- Input: `head = [1], n = 1`
|
||||
- Output: `[]`
|
||||
|
||||
### Example 3:
|
||||
- Input: `head = [1,2], n = 1`
|
||||
- Output: `[1]`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the list is sz.
|
||||
- `1 <= sz <= 30`
|
||||
- `0 <= Node.val <= 100`
|
||||
- `1 <= n <= sz`
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def removeNthFromEnd(self, head: ListNode | None, n: int) -> ListNode | None:
|
||||
dummy = ListNode(0)
|
||||
dummy.next = head
|
||||
|
||||
fast = dummy
|
||||
for _ in range(n):
|
||||
if fast.next is None:
|
||||
return head
|
||||
fast = fast.next
|
||||
|
||||
slow = dummy
|
||||
while fast.next is not None:
|
||||
slow = slow.next
|
||||
fast = fast.next
|
||||
|
||||
slow.next = slow.next.next
|
||||
|
||||
return dummy.next
|
||||
```
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
---
|
||||
title: '82. Remove Duplicates from Sorted List II'
|
||||
description: You are given the head of a sorted linked list
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Linked List", slug: "linked-list" }
|
||||
- { name: "Two Pointers", slug: "two-pointers" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/84"
|
||||
sidebar:
|
||||
label: 'Remove Duplicates from Sorted List II'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
### Example 1:
|
||||
- Input: `head = [1,2,3,3,4,4,5]`
|
||||
- Output: `[1,2,5]`
|
||||
|
||||
### Example 2:
|
||||
- Input: `head = [1,1,1,2,3]`
|
||||
- Output: `[2,3]`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the list is in the range [0, 300].
|
||||
- `-100 <= Node.val <= 100`
|
||||
- The list is guaranteed to be sorted in ascending order.
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def deleteDuplicates(self, head: ListNode | None) -> ListNode | None:
|
||||
sentinel = ListNode(0, head)
|
||||
pred = sentinel
|
||||
|
||||
while head:
|
||||
if head.next and head.val == head.next.val:
|
||||
while head.next and head.val == head.next.val:
|
||||
head = head.next
|
||||
|
||||
pred.next = head.next
|
||||
else:
|
||||
pred = pred.next
|
||||
|
||||
head = head.next
|
||||
|
||||
return sentinel.next
|
||||
```
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
title: '876. Middle of the Linked List'
|
||||
description: Given the head of a singly linked list, return the middle node of the linked list
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Linked List", slug: "linked-list" }
|
||||
- { name: "Two Pointers", slug: "two-pointers" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/82"
|
||||
sidebar:
|
||||
label: 'Middle of the Linked List'
|
||||
badge: 'Easy'
|
||||
---
|
||||
|
||||
### Example 1:
|
||||
- Input: `head = [1,2,3,4,5]`
|
||||
- Output: `[3,4,5]`
|
||||
- Explanation: The middle node of the list is node `3`.
|
||||
|
||||
### Example 2:
|
||||
- Input: `head = [1,2,3,4,5,6]`
|
||||
- Output: `[4,5,6]`
|
||||
- Explanation: Since the list has two middle nodes with values `3` and `4`, we return the second one.
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the list is in the range [1, 100].
|
||||
- `1 <= Node.val <= 100`
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def middleNode(self, head: ListNode | None) -> ListNode | None:
|
||||
slow = fast = head
|
||||
while fast and fast.next:
|
||||
fast = fast.next.next
|
||||
slow = slow.next
|
||||
|
||||
return slow
|
||||
```
|
||||
@@ -0,0 +1,65 @@
|
||||
---
|
||||
title: '92. Reverse Linked List II'
|
||||
description: Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list
|
||||
icon: dot
|
||||
topics:
|
||||
- { name: "Linked List", slug: "linked-list" }
|
||||
issue: "https://github.com/prdlk/leetcode/issues/83"
|
||||
sidebar:
|
||||
label: 'Reverse Linked List II'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
::::warning
|
||||
Could you do it in one pass?
|
||||
::::
|
||||
|
||||
### Example 1:
|
||||
- Input: `head = [1,2,3,4,5], left = 2, right = 4`
|
||||
- Output: `[1,4,3,2,5]`
|
||||
|
||||
### Example 2:
|
||||
- Input: `head = [5], left = 1, right = 1`
|
||||
- Output: `[5]`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the list is n.
|
||||
- `1 <= n <= 500`
|
||||
- `-500 <= Node.val <= 500`
|
||||
- `1 <= left <= right <= n`
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# Definition for singly-linked list.
|
||||
# class ListNode:
|
||||
# def __init__(self, val=0, next=None):
|
||||
# self.val = val
|
||||
# self.next = next
|
||||
class Solution:
|
||||
def reverseBetween(
|
||||
self, head: ListNode | None, left: int, right: int
|
||||
) -> ListNode | None:
|
||||
cur, prev = head, None
|
||||
while left > 1:
|
||||
prev = cur
|
||||
cur = cur.next
|
||||
left, right = left - 1, right - 1
|
||||
|
||||
tail, con = cur, prev
|
||||
while right:
|
||||
third = cur.next
|
||||
cur.next = prev
|
||||
prev = cur
|
||||
cur = third
|
||||
right -= 1
|
||||
|
||||
if con:
|
||||
con.next = prev
|
||||
else:
|
||||
head = prev
|
||||
|
||||
tail.next = cur
|
||||
return head
|
||||
```
|
||||
Reference in new issue
Block a user