(1) If we can access the node directly, we can override its value with the value of its next node. This won't work if the node to be deleted is at the tail of the list
// start and end are used as sentinels here, we are going to reverse the part between start node
// and end node
public ListNode reverse(ListNode start, ListNode end) {
ListNode last = start.next;
ListNode curNode = last.next;
while (curNode != end) {
last.next = curNode.next;
curNode.next = start.next;
start.next = curNode;
curNode = last.next;
}
return last;
}
3. Split a list into two halves
Use two pointers (fast and slow) to split a list into halves
public void splitList(ListNode head) {
if (head == null || head.next == null) {
return;
}
ListNode fast = head, slow = head;
while (fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
// Get the head of the second half
ListNode secondHalf = slow.next;
// Cut the list
slow.next = null;
}