Niuke.com High-Frequency Algorithm Questions Series – BM2 – Reversal of Specified Intervals in Linked Lists

Time:2022-8-4

Niuke.com High-Frequency Algorithm Questions Series – BM2 – Reversal of Specified Intervals in Linked Lists

Topic description

Inverting the interval from position m to position n in a linked list of size size requires time complexity O(n) and space complexity O(1).

See original title:Reverse the specified range in the BM2 linked list

Solution 1: linked list traversal, pointer exchange

Because the starting position may be the head node, first set up a virtual head node dummyNode and point next to the original head node, and then the processing process is as follows:

  • First traverse the linked list to find the previous node pre at the starting position m, which is used to record the node before the reversal;
  • Then use cur and next to record the next node of pre, and use next to record the next node of cur;
  • Then continue to traverse the linked list, transfer the next node to the next node of the pre node by exchanging the next pointers of pre, next, and cur, and then process the next node of cur in a loop;
  • Traversing to the node at the end position n is the end of the inversion.
  • Finally, the next node that returns the dummyNode node is the reversed linked list.
public class Bm002 {
    /**
     * @param head ListNode class
     * @param m starting position
     * @param n end position
     * @return ListNode class
     */
    public static ListNode reverseBetween(ListNode head, int m, int n) {
        if (head == null || head.next == null) {
            return head;
        }
        if (m == n) {
            return head;
        }
        // set the virtual head node
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        // pre is the previous node of the reversal interval
        ListNode pre = dummyNode;
        for (int i = 0; i < m - 1; i++) {
            pre = pre.next;
        }
        // cur is initially the start node of the reversal interval
        ListNode cur = pre.next;
        ListNode next;
        for (int i = 0; i < n - m; i++) {
            // By exchanging the next pointer, move the next node to the next node of the pre node
            next = cur.next;
            cur.next = next.next;
            next.next = pre.next;
            pre.next = next;
        }

        // Finally, the next node that returns the dummyNode node is the reversed linked list
        return dummyNode.next;
    }

    public static void main(String[] args) {
        ListNode head = ListNode.testCase2();
        System.out.println(&quot;Before the specified interval is reversed&quot;);
        ListNode.print(head);

        ListNode newHead = reverseBetween(head, 2, 4);
        System.out.println(&quot;After the specified interval is reversed&quot;);
        ListNode.print(newHead);
    }
}

$1.01^{365} ≈ 37.7834343329$
$0.99^{365} ≈ 0.02551796445$
Believe in the power of persistence!

Recommended Today

Leetcode PHP题解–D127 455. Assign Cookies

D127 455. Assign Cookies topic link 455. Assign Cookies topic analysis Given two arrays, the first array represents the capacity of each container, and the second array represents the number of each item. The items need to be stuffed into the container, but there are two conditions. Condition 1: Only one item can be stuffed […]