首页 > 代码库 > LeetCode Reverse Nodes in k-Group

LeetCode 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

思路分析:这题属于反转单链表的变形题目,首先我们要搞清楚如何反转单链表,比较好写的方法是定义pre,cur,after三个指针,我们要做的就是iter链表,试图把每个当前的cur node都“插入”到链表“头部”之前,为了方便操作,我们维护pre,cur,after三个指针,同时用dummy来维护新的头结点,这样可以方便我们不断向“头部”之前插入新的node。如下函数可以反转任意给定两个node之间的链表(不包含这两个node),返回被反转部分的第一个node。注意实现这个函数时,要保证反转之后两端仍然与beginNode和endNode正确链接,最后那句beginNode.next = dummy就是链接beginNode和反转之后新的头结点。这个bug浪费了不少时间,以后要更加仔细。

//reverse nodes between beginNode and endNode(exclusively)
	//return the first node in the reversed part
	private static ListNode reverseLinkedList(ListNode beginNode, ListNode endNode) {
		// TODO Auto-generated method stub
		ListNode head = beginNode.next;
		ListNode dummy = head;//use dummy to maintain the new head
		ListNode pre = head;
		ListNode cur = pre.next;
		ListNode after = cur.next;
		while(cur != endNode){
			pre.next = after;
			cur.next = dummy;
			dummy = cur;
			cur = pre.next;
			if(cur == null) break;
			after = cur.next;
		}
		beginNode.next = dummy;//!after reverse, beginNode should also before the first Node, endNode should also before the last node
		return dummy;
	}
然后我们就可以调用这个函数来解决本题,需要定义一个fakeHead,其next成员是真正的head,作为初始的beginNode。我们iter链表,每经过k个node做一次反转操作,调用上面的函数,要注意传入正确的beginNode和endNode,同时注意更新head,返回反转完成之后新的head。时间复杂度O(N)。

AC Code

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        if(head == null || head.next == null || k < 2) return head;
        ListNode fakeHead = new ListNode(-1);
		fakeHead.next = head;
		ListNode pre = fakeHead;
		ListNode cur = head;
		int i = 0;
		while(cur != null){
			i++;
			cur = cur.next;
			if(i % k == 0){
				pre = reverseLinkedList(pre, cur);
				if(i == k){
					head = pre;
				}
				int temp = k;
				while(temp > 1){
					pre = pre.next;
					temp--;
				}
			}
		}
		return head;
    }
    
    //reverse nodes between beginNode and endNode(exclusively)
	//return the first node in the reversed part
	private static ListNode reverseLinkedList(ListNode beginNode, ListNode endNode) {
		// TODO Auto-generated method stub
		ListNode head = beginNode.next;
		ListNode dummy = head;//use dummy to maintain the new head
		ListNode pre = head;
		ListNode cur = pre.next;
		ListNode after = cur.next;
		while(cur != endNode){
			pre.next = after;
			cur.next = dummy;
			dummy = cur;
			cur = pre.next;
			if(cur == null) break;
			after = cur.next;
		}
		beginNode.next = dummy;//!after reverse, beginNode should also before the first Node, endNode should also before the last node
		return dummy;
	}
}



LeetCode Reverse Nodes in k-Group