(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 nodepublicListNodereverse(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
publicvoidsplitList(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 halfListNode secondHalf =slow.next;// Cut the listslow.next=null; }