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]
Iterate through the list and reverse the links between each node.
Create 3 variables, current (which starts off at head), and the pointers prev and next (which are both null to start).
Current is the node you’re currently on. Any time you adjust prev and next, you’re adjusting the pointers.
Do 4 things with each node…
Finally, assign head to prev to change the head and return head.
You are given the heads of two sorted linked lists list1 and list2.
Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.
Return the head of the merged linked list.
Example 1
Input: list1 = [1,2,4], list2 = [1,3,4]
Output: [1,1,2,3,4,4]
Example 2:
Input: list1 = [], list2 = []
Output: []
Use a dummy node to keep track of the previous value (we need this because there is no previous value at first).
Assign a variable prev to dummy (this is what will keep track of the list).
Check to see if l1 > l2.
Use a while looping checking to see if l1 and l2 still have values and…
Then, when one list has run out, connect prev (the last value to be added to the merged list), to the remaining list.
if (!l1) prev.next = l2
if (!l2) prev.next = l1
Finally, return dummy.next, which holds a record of the head of the list.
Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Floyd’s cycle finding algorithm aka the tortoise and the hare approach,
have two pointers going at different speeds. If there’s a cycle, eventually they will land on the same node