Given the head
of a singly linked list, return the middle node of the linked list.
If there are two middle nodes, return the second middle node.
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:
[1, 100]
.1 <= Node.val <= 100
program main
! Solves the problem "Given the 'head' of a singly linked list, return the middle node of the linked list."
implicit none
type :: node_t
integer :: val
type(node_t), pointer :: next => null()
end type
type(node_t), target :: head
type(node_t), pointer :: current => null(), mid => null()
integer :: i
! Examples
call add_node(head, 1)
call add_node(head, 2)
call add_node(head, 3)
call add_node(head, 4)
call add_node(head, 5)
print "(A)", get_middle_node(head)%val
call delete_all_nodes(head)
call add_node(head, 1)
call add_node(head, 2)
call add_node(head, 3)
call add_node(head, 4)
call add_node(head, 5)
call add_node(head, 6)
print "(A)", get_middle_node(head)%val
call delete_all_nodes(head)
contains
subroutine add_node(head, val)
! Adds a new node with value `val` at the beginning of the linked list.
type(node_t), target :: head
integer, intent(in) :: val
type(node_t), pointer :: new_node
allocate(new_node)
new_node%val = val
new_node%next => head%next
head%next => new_node
end subroutine
function get_middle_node(head) result(mid)
! Returns the middle node of the linked list. If there are two middle nodes, returns the second one.
type(node_t), target :: head
type(node_t), pointer :: current, mid
current => head%next
mid => head%next
do while (associated(current))
if (associated(current%next)) then
current => current%next%next
else
exit
end if
mid => mid%next
end do
end function
subroutine delete_all_nodes(head)
! Deletes all nodes from the linked list.
type(node_t), target :: head
type(node_t), pointer :: current, next
current => head%next
do while (associated(current))
next => current%next
deallocate(current)
current => next
end do
end subroutine
end program
temp.f95:20:40: 20 | print "(A)", get_middle_node(head)%val | 1 Error: Symbol ‘get_middle_node’ at (1) has no IMPLICIT type temp.f95:29:40: 29 | print "(A)", get_middle_node(head)%val | 1 Error: Symbol ‘get_middle_node’ at (1) has no IMPLICIT type
module linked_list
type :: node
integer :: val
type(node), pointer :: next => null()
end type
interface
function get_middle(head) result(middle)
type(node), pointer, intent(in) :: head
type(node), pointer :: middle
end function
end interface
contains
function get_middle(head) result(middle)
type(node), pointer, intent(in) :: head
type(node), pointer :: current, middle
! Initialize current to head
current => head
! Loop through the list until we reach the end
do while (associated(current%next))
! If we have not found the middle node yet,
! move to the next node
if (.not. associated(middle)) then
middle => current
else
middle => middle%next
end if
current => current%next
end do
! Return the middle node
middle => middle%next
end function
end module
program test_linked_list
use linked_list
implicit none
! Create a linked list with 5 nodes
type(node), pointer :: head, current
allocate(head)
head%val = 1
current => head
do i = 2, 5
allocate(current%next)
current%next%val = i
current => current%next
end do
! Print the middle node of the list
write (*,*) "Middle node: ", get_middle(head)%val
! Create a linked list with 6 nodes
deallocate(head)
allocate(head)
head%val = 1
current => head
do i = 2, 6
allocate(current%next)
current%next%val = i
current => current%next
end do
! Print the middle node of the list
write (*,*) "Middle node: ", get_middle(head)%val
end program
temp.f95:10:19: 10 | type(node), pointer, intent(in) :: head | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:11:19: 11 | type(node), pointer :: middle | 1 Error: Derived type ‘node’ at (1) is being used before it is defined temp.f95:17:20: 9 | function get_middle(head) result(middle) | 2 ...... 17 | function get_middle(head) result(middle) | 1 Error: Procedure ‘get_middle’ at (1) is already defined at (2) temp.f95:18:43: 18 | type(node), pointer, intent(in) :: head | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:19:42: 19 | type(node), pointer :: current, middle | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:22:19: 22 | current => head | 1 Error: Unexpected pointer assignment statement in CONTAINS section at (1) temp.f95:25:34: 25 | do while (associated(current%next)) | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:28:42: 28 | if (.not. associated(middle)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:29:29: 29 | middle => current | 1 Error: Unexpected pointer assignment statement in CONTAINS section at (1) temp.f95:30:12: 30 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:31:30: 31 | middle => middle%next | 1 Error: Symbol ‘middle’ at (1) has no IMPLICIT type temp.f95:32:11: 32 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:33:28: 33 | current => current%next | 1 Error: Symbol ‘current’ at (1) has no IMPLICIT type temp.f95:34:7: 34 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:37:22: 37 | middle => middle%next | 1 Error: Symbol ‘middle’ at (1) has no IMPLICIT type temp.f95:38:3: 38 | end function | 1 Error: Expecting END MODULE statement at (1) temp.f95:43:9: 43 | use linked_list | 1 Fatal Error: Cannot open module file ‘linked_list.mod’ for reading at (1): No such file or directory compilation terminated.
from collections import Counter
def is_n_straight_hand(hand, group_size):
counts = Counter(hand)
for card in sorted(counts):
if counts[card] > 0:
for i in range(group_size - 1, -1, -1):
if counts[card + i] < counts[card]:
return False
counts[card + i] -= counts[card]
return True
The algorithm uses a hashmap (Python's `Counter` class, or `Map` class in JavaScript) to count the occurrences of each card value in the hand. Then, it iterates through the card values in sorted order (using `sorted()` for Python and `sort()` for JavaScript). For each card value with positive count, it checks that it can form a group of consecutive cards of size `groupSize`. If any of these groups cannot be formed, it returns False, otherwise returns True. This approach takes advantage of the sorted order to ensure that the consecutive groups are formed in a optimal way.
\
#include <map>
#include <vector>
using namespace std;
bool isNStraightHand(vector<int>& hand, int groupSize) {
map<int, int> counts;
for (int card : hand) {
counts[card]++;
}
for (auto it = counts.begin(); it != counts.end(); ++it) {
if (it->second > 0) {
for (int i = groupSize - 1; i >= 0; --i) {
if (counts[it->first + i] < it->second) {
return false;
}
counts[it->first + i] -= it->second;
}
}
}
return true;
}
The algorithm uses a hashmap (Python's `Counter` class, or `Map` class in JavaScript) to count the occurrences of each card value in the hand. Then, it iterates through the card values in sorted order (using `sorted()` for Python and `sort()` for JavaScript). For each card value with positive count, it checks that it can form a group of consecutive cards of size `groupSize`. If any of these groups cannot be formed, it returns False, otherwise returns True. This approach takes advantage of the sorted order to ensure that the consecutive groups are formed in a optimal way.
\