# Posts published in “Linked List Operations”

Given the `head` of a linked list, return the list after sorting it in ascending order.

Follow up: Can you sort the linked list in `O(n logn)` time and `O(1)` memory (i.e. constant space)?

Example 1:

```Input: head = [4,2,1,3]
Output: [1,2,3,4]
```

Example 2:

```Input: head = [-1,5,3,4,0]
Output: [-1,0,3,4,5]
```

Example 3:

```Input: head = []
Output: []
```

Constraints:

• The number of nodes in the list is in the range `[0, 5 * 104]`.
• `-105 <= Node.val <= 105`

Idea:

Merge Sort (use slow and fast pointer to find the middle of the linked list and split them)

Time Complexity: O(nlogn)

Solution:

``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @return {ListNode}
*/
// empty or one node
let right = sortList(mid);
return merge(left, right);
};

function merge(list1, list2) {
let tail = new ListNode();
while (list1 !== null && list2 !== null) {
if (list1.val < list2.val) {
tail.next = list1;
list1 = list1.next;
tail = tail.next;
} else {
tail.next = list2;
list2 = list2.next;
tail = tail.next;
}
}
tail.next = (list1 !== null) ? list1 : list2;
};

while (fast !== null && fast.next !== null) {
slow = slow.next
fast = fast.next.next;
}
// !!!Important here:
// disconnect, split them into two lists
return secondHalf;
}``````

Given the `head` of a singly linked list, reverse the list, and return the reversed list.

Example 1:

```Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
```

Example 2:

```Input: head = [1,2]
Output: [2,1]
```

Example 3:

```Input: head = []
Output: []
```

Constraints:

• The number of nodes in the list is the range `[0, 5000]`.
• `-5000 <= Node.val <= 5000`

Idea: 背口诀

```reverse LinkedList 口诀: create newHead and next

Solution:

``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @return {ListNode}
*/
// empty or only one node

let next = new ListNode();
// next helper create
// disconnect, and point to newHead.next
// move head to next location
}
};``````

You are given the `root` of a binary search tree (BST) and an integer `val`.

Find the node in the BST that the node’s value equals `val` and return the subtree rooted with that node. If such a node does not exist, return `null`.

Example 1:

```Input: root = [4,2,7,1,3], val = 2
Output: [2,1,3]
```

Example 2:

```Input: root = [4,2,7,1,3], val = 5
Output: []
```

Constraints:

• The number of nodes in the tree is in the range `[1, 5000]`.
• `1 <= Node.val <= 107`
• `root` is a binary search tree.
• `1 <= val <= 107`

Solution:

``````/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
*     this.val = (val===undefined ? 0 : val)
*     this.left = (left===undefined ? null : left)
*     this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number} val
* @return {TreeNode}
*/
var searchBST = function(root, val) {
if (root === null) return null
if (root.val === val)
return root;
else if (val < root.val)
return searchBST(root.left, val);
else
return searchBST(root.right, val);
};``````

Example 1:

```Input: head = [1,2,3,4]
Output: [2,1,4,3]
```

Example 2:

```Input: head = []
Output: []
```

Example 3:

```Input: head = [1]
Output: [1]
```

Constraints:

• The number of nodes in the list is in the range `[0, 100]`.
• `0 <= Node.val <= 100`

Solution:

``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @return {ListNode}
*/
if (head === null) return null;
}
return dummy;
};

}``````

Write an algorithm to determine if a number `n` is happy.

happy number is a number defined by the following process:

• Starting with any positive integer, replace the number by the sum of the squares of its digits.
• Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1.
• Those numbers for which this process ends in 1 are happy.

Return `true` if `n` is a happy number, and `false` if not.

Example 1:

```Input: n = 19
Output: true
Explanation:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
```

Example 2:

```Input: n = 2
Output: false
```

Constraints:

• `1 <= n <= 231 - 1`

Idea:

create a function to calculate the sum of the squares of its digits.

use fast and slow pointers traversal concept (the other one execute function twice) to check if they can reach 1 (means it is happy number)

Solution:

``````/**
* @param {number} n
* @return {boolean}
*/
var isHappy = function(n) {
let slow = n;
let fast = n;

do {
slow = squareSum(slow);
fast = squareSum(squareSum(fast));
} while (slow !== fast);

return fast === 1;
};

var squareSum = function(n) {
let sum = 0;
while(n) {
sum += (n % 10) * (n % 10);
n = parseInt(n / 10);
}
return sum;
};``````

Given the `head` of a linked list, remove the `nth` node from the end of the list and return its head.

Follow up: 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`
``````/**
* function ListNode(val, next) {
*     this.val = (val===undefined ? 0 : val)
*     this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {
return null;

let first = new ListNode();
let second = first;
// first point to n + 1
first = first.next;
while (n > 0) {
first = first.next;
n--;
}
// first already reach to the end null, means need to remove the first node
if (first === null) {
} else {
// maintaining N nodes apart between first and second pointer
while (first !== null) {
first = first.next;
second = second.next;
}
// Now second pointer to the node that need to be removed
// remove that node now
second.next = second.next.next;
}