This page looks best with JavaScript enabled

LinkedList reversing

 ·  ☕ 2 min read · 👀... views

Summary on LinkedList reversing.

Starter

Special case: LC206 reverse all the nodes in one pass:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public ListNode reverseList(ListNode head) {
    ListNode newHead = null;
    while (head != null) {
        ListNode next = head.next;
        head.next = newHead;
        newHead = head;
        head = next;
    }
    return newHead;
}

General case: LC92 reverse nodes from position m to n

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
public ListNode reverseBetween(ListNode head, int m, int n) {
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode pre = dummy, end, cur;
    for (int i = 0; i < m-1; i++) { pre = pre.next; }
	end = pre.next;
    cur = end.next;
    for (int i = 0; i < n - m; i++) {
        end.next = cur.next;
        cur.next = pre.next;
        pre.next = cur;
        cur = end.next;
    }
    return dummy.next;
}

Main dish: LC25 Reverse Nodes in k-Groups

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * Reverse a link list between pre and next exclusively
 * e.g.: 
 * 0->1->2->3->4->5->6
 * |           |   
 * pre        next
 *
 * after call pre = reverse(pre, next):
 * 0->3->2->1->4->5->6
 *          |  |
 *          pre next
 * @param pre 
 * @param next
 * @return the precedence of parameter next
 */
private static ListNode reverse(ListNode pre, ListNode next) {
    ListNode end = pre.next, cur = end.next;
    while (cur != next) {
        end.next = cur.next;
        cur.next = pre.next;
        pre.next = cur;
        cur = end.next;
    }
    return end;
}
public ListNode reverseKGroup(ListNode head, int k) {
    if (head == null || k == 1) return head;
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode pre = dummy;
    int i = 0;
    while (head != null) {
        head = head.next;            
        if (++i % k == 0) pre = reverse(pre, head);
    }
    return dummy.next;
}
/**
 * Note: the while loop is the same as following process, which is easier to understand:
 * while(head != null){
 *    i++;
 *    if(i % k ==0){
 *        pre = reverse(pre, head.next);
 *        head = pre.next;
 *    }else {
 *        head = head.next;
 *    }
 * }
 */

}
Share on
Support the author with