链表翻转系列

Problem I

翻转链表

程序(简洁版)

非递归

ListNode reverseList(ListNode head) {
	ListNode pre = null;
	ListNode cur = head;
	
	while (cur != null ) {
		ListNode next = cur.next;
		cur.next = pre;
		pre = cur;
		cur = next;
	}
	
	return pre;
}

Problem II: Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

依次交换链表的一对节点。

程序

public class Solution {
    public ListNode swapPairs(ListNode head) {
		ListNode dummy = new ListNode(-1);
		ListNode pre = head;
		ListNode connect = dummy;
		
		while (pre != null && pre.next != null) {
			ListNode next = pre.next;
			ListNode cross = next.next;
			next.next = pre;
			pre.next = cross;
			connect.next = next;
			connect = pre;
			pre = cross;
		}
		
		return dummy.next == null ? head : dummy.next;
	}
}

Problem III: Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

程序

public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if (head == null || head.next == null || k <= 1) {
            return head;
        }
        
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode cur = head;
        int cnt = 0;
        
        while (cur != null) {
            ++cnt;
            if (cnt % k == 0) {
                // reverse group
                ListNode last = reverseGroup(pre, cur.next);
                pre = last;
                cur = last.next;
            } else {
                cur = cur.next;
            }
        }
        
        return dummy.next;
    }
    
    private ListNode reverseGroup(ListNode pre, ListNode next) {
        ListNode last = pre.next;
        ListNode cur = last.next;
        
        while (cur != next) {
            last.next = cur.next;
            cur.next = pre.next;
            pre.next = cur;
            cur = last.next;
        }
        
        return last;
    }
}

难点在于每次是翻转两个节点之间的节点们。

Problem IV: Reverse LinkedList II

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:
Given 1->2->3->4->5->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

程序

public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        
        while (--m > 0) {
            pre = pre.next;
        }
        
        ListNode next = head;
        while (--n > 0) {
            next = next.next;
        }
        
        reverseBetween(pre, next.next);
        return dummy.next;
    }
    
    private void reverseBetween(ListNode pre, ListNode next) {
        ListNode last = pre.next;
        ListNode cur = last.next;
        
        while (cur != next) {
            last.next = cur.next;
            cur.next = pre.next;
            pre.next = cur;
            cur = last.next;
        }
    }
}

找准位置,套用问题3的方法即可~

原文地址:https://www.cnblogs.com/harrygogo/p/4716237.html