1. 程式人生 > >( Leetcode 25 ) Reverse Nodes in k-Group

( Leetcode 25 ) 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

解題思想:

首先題目的意思是說給定一個連結串列和一個數k,將連結串列以k個結點為一組進行反轉,如果不夠k個結點則不進行反轉。主要分為以下幾步來進行解題:

1.  將連結串列以k個結點為一組進行拆分,如果夠k個結點則進行反轉

2.  如果不夠k個結點則不進行反轉

3.  拼接階段,將反轉後的一小段連結串列插入到已經操作過的連結串列中間,如果不夠k個結點則不操作。

具體見下面的Java程式碼(有詳細的註釋):

public class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
		if( head == null || head.next == null ){
			return head;
		}
		
		ListNode p = head;
		ListNode pre = null;        //pre 記錄上一個半段連結串列的尾結點
		boolean flag = true;        //為是否是頭結點做標記
		while( p != null ){
			ListNode tail = p;     //當前是頭,但是反過來就不是頭了,成了尾部
			int i;
			for( i = 1; i < k && p != null; ++i ){
				p = p.next;
			}
			
			ListNode nextList = null;
			if( i >= k && p != null ){
				nextList = p.next;
				p.next = null;   //將當前連結串列的最後一個節點next置為null
				ListNode tempHead = reverse(tail);
				if( flag == true ){  //如果頭,進行處理
					head = tempHead;
					flag = false;
					tail.next = nextList;
				}else{             //如果不是頭的話
					tail.next = nextList;
					pre.next = tempHead;
				}
			}
			pre = tail;     //記錄上一段連結串列的尾結點
			p = nextList;
		}
		
		return head;
    }
	
	public ListNode reverse( ListNode head ){
		if( head == null || head.next == null ){
			return head;
		}
		ListNode p = head, q = head.next;
		head.next = null;
		while( q != null ){
			ListNode r = q.next;
			q.next = p;
			
			p = q;
			q = r;
		}
		head = p;
		return head;
	}
}


相關推薦

Leetcode 25:Reverse Nodes in k-Group

pty 指針 pos top multiple 插入 num class break 1.題目描述 2.解題思路 3.代碼實現 1.題目描述 Given a linked list, reverse the nodes of a linked list k at

(Java) LeetCode 25. Reverse Nodes in k-Group —— k個一組翻轉鏈表

it is modified example mod pre you brush itself int Given a linked list, reverse the nodes of a linked list k at a time and return its mo

Leetcode.25. Reverse Nodes in k-Group

null next cbc 有一點 邏輯 back 進行 hid ++ 題意:鏈表翻轉,每k個翻一次,最後不足k個的不翻 題解:沒啥難點,就是要對邏輯有一點要求;當然你也可以先存到數組裏,然後用數學方法計算下標進行swap,但是這脫離了這道題的本意,代碼不放了 1 c

[leetcode]25. Reverse Nodes in k-Group

這題是真的hard。我卡了好久沒有想明白。 這個迭代解法太絕妙了: https://leetcode.com/problems/reverse-nodes-in-k-group/discuss/11440/Non-recursive-Java-solution-and-idea pu

python leetcode 25. Reverse Nodes in k-Group

按著題意做即可,遍歷找到所在位置再翻轉 class Solution(object): def reverseKGroup(self, head, k): """ :type head: ListNode :type k: int

LeetCode 25Reverse Nodes in k-Group(k個一組翻轉連結串列)

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

[LeetCode]25. Reverse Nodes in k-Group k個一組翻轉連結串列

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. k is a positive integer and is less than or equ

[LeetCode]25. Reverse Nodes in k-Group k個一組翻轉鏈表

結點 遞歸 out 鏈表翻轉 ext its count nod link Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

( Leetcode 25 ) 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 multip

leetcode:25. 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 multipl

LeetCode 25 Reverse Nodes in k-Group (C,C++,Java,Python)

Problem: Given a linked list, reverse the nodes of a linked list k at a time and return its modifi

(Java)LeetCode-25. 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 mult

#Leetcode# 25. Reverse Nodes in k-Group

rem linked ans brush it is style ref ron != https://leetcode.com/problems/reverse-nodes-in-k-group/ Given a linked list, reverse the

LeetCode算法題python解法:25. Reverse Nodes in k-Group

value it is lee etc multipl constant elif The itself Given a linked list, reverse the nodes of a linked list k at a time and return its m

LeetCode25. Reverse Nodes in k-Group - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Given a linked list, reverse the nodes of a linked list k at a time and

25. Reverse Nodes in k-Group

art nbsp toc count tno current have ren ini 第二輪最後interviewer 直接說you are done for today1st round:-google 1point3acresyou have 2 available

25. Reverse Nodes in k-Group(need constant approach)

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

25 reverse-nodes-in-k-group

這道題的大意是說,給一個連結串列,再給一個整型數字K,要實現這個連結串列每K個數進行一次反轉,整條連結串列反轉的題目比較熟悉,但是,一個連結串列分成好幾組進行反轉其實大體思路是一樣的,但是每組反轉結束之後,應該如何變化指標進行下一次反轉,這是這道題的重點。 按照以前連結串列反轉的思路,我們首先要

LeetCodeReverse Nodes in k-Group

題目連結: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 nu

25 Reverse Nodes in k-Group

class Solution { public: ListNode* reverseKGroup(ListNode* head, int k) { bool flag = false; bool chk = false; ListNode *p; ListNode *tem