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.
[1, 100]
.1 <= Node.val <= 100
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var middleNode = function(head) {
let node = head;
let answer = head;
let nodeCount = 0;
while (node) {
node = node.next;
nodeCount++;
}
let middleIndex = Math.floor(nodeCount / 2);
while (middleIndex) {
answer = answer.next;
middleIndex--;
}
return answer;
};
head
of a linked list, remove the nth
node from the end of the list and return its head.Example 1:
Example 2:
sz
.1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {
let node = head;
let nodeCount = 0;
while (node) {
node = node.next;
nodeCount++;
}
if (nodeCount === n) {
head = head.next;
return head;
}
let removedNodeIndex = nodeCount - n - 1;
node = head;
nodeCount = 0;
while (node) {
if (nodeCount === removedNodeIndex) {
node.next = node.next.next;
}
node = node.next;
nodeCount++;
}
return head;
};
head
, swap every two adjacent nodes and return its head. Example 1:
Example 2:
Example 3:
[0, 100]
.0 <= Node.val <= 100
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var swapPairs = function(head) {
let answer = new ListNode(0);
let node = answer;
answer.next = head;
if (!head || !head.next) {
return head;
}
while (node.next && node.next.next) {
const node1 = node.next;
const node2 = node.next.next;
node.next = node2;
node1.next = node2.next;
node2.next = node1;
node = node.next.next;
}
return answer.next;
};
Given the head
of a linked list head, in which each node contains an integer value.
Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them.
Return the linked list after insertion.
The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
[1, 5000]
.1 <= Node.val <= 1000
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var insertGreatestCommonDivisors = function(head) {
const getGreatestCommonDivisors = (num1, num2) => {
const smallNumber = num1 > num2 ? num1 : num2;
for (let i = smallNumber; i > 1; i--) {
if (num1 % i === 0 && num2 % i === 0) {
return i;
}
}
return 1;
};
let node = head;
while (node && node.next) {
const node1 = node;
const node2 = node.next;
const greatestCommonDivisors = getGreatestCommonDivisors(node1.val, node2.val);
const divisorNode = new ListNode(greatestCommonDivisors);
node1.next = divisorNode;
divisorNode.next = node2;
node = node2;
}
return head;
};