日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

K个一组翻转链表—leetcode25

發(fā)布時間:2024/4/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 K个一组翻转链表—leetcode25 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

給你一個鏈表,每?k?個節(jié)點一組進行翻轉(zhuǎn),請你返回翻轉(zhuǎn)后的鏈表。

k?是一個正整數(shù),它的值小于或等于鏈表的長度。

如果節(jié)點總數(shù)不是?k?的整數(shù)倍,那么請將最后剩余的節(jié)點保持原有順序。

示例:

給你這個鏈表:1->2->3->4->5

當(dāng)?k?= 2 時,應(yīng)當(dāng)返回:?2->1->4->3->5

當(dāng)?k?= 3 時,應(yīng)當(dāng)返回:?3->2->1->4->5

說明:

  • 你的算法只能使用常數(shù)的額外空間。
  • 你不能只是單純的改變節(jié)點內(nèi)部的值,而是需要實際進行節(jié)點交換。

/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/ class Solution { public:ListNode* reverseKGroup(ListNode* head, int k) {if(k==1)return head;ListNode* tempHead = head;int length = 0;while(tempHead!=NULL){length++;tempHead = tempHead->next;}tempHead = head;vector<ListNode*> vec;int index = 0;ListNode* temp = NULL;while(index+k<=length){for(int i=0;i<k-1;++i){if(i==0)vec.push_back(tempHead);tempHead = tempHead->next;}temp = tempHead->next;tempHead->next = NULL;tempHead = temp;index+=k;}if(!vec.empty()){int len = vec.size();ListNode* node = NULL;ListNode* result = NULL;if(len>1){for(int i=0;i<len-1;++i){node = reverse(vec[i]);if(i==0)result = node;while(node->next!=NULL)node = node->next;node->next = reverse(vec[i+1]);}}else{node = reverse(vec[0]);result = node;}while(node->next!=NULL)node = node->next;node->next = tempHead;return result;}return head;}ListNode* reverse(ListNode* head){ListNode* t = head;ListNode* newHead = NULL;while(t!=NULL){ListNode* temp = t->next;t->next = newHead;newHead = t;t = temp;}return newHead;} };

?方法二:

/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/ class Solution {public ListNode reverseKGroup(ListNode head, int k) {if (head == null || head.next == null){return head;}//定義一個假的節(jié)點。ListNode dummy=new ListNode(0);//假節(jié)點的next指向head。// dummy->1->2->3->4->5dummy.next=head;//初始化pre和end都指向dummy。pre指每次要翻轉(zhuǎn)的鏈表的頭結(jié)點的上一個節(jié)點。end指每次要翻轉(zhuǎn)的鏈表的尾節(jié)點ListNode pre=dummy;ListNode end=dummy;while(end.next!=null){//循環(huán)k次,找到需要翻轉(zhuǎn)的鏈表的結(jié)尾,這里每次循環(huán)要判斷end是否等于空,因為如果為空,end.next會報空指針異常。//dummy->1->2->3->4->5 若k為2,循環(huán)2次,end指向2for(int i=0;i<k&&end != null;i++){end=end.next;}//如果end==null,即需要翻轉(zhuǎn)的鏈表的節(jié)點數(shù)小于k,不執(zhí)行翻轉(zhuǎn)。if(end==null){break;}//先記錄下end.next,方便后面鏈接鏈表ListNode next=end.next;//然后斷開鏈表end.next=null;//記錄下要翻轉(zhuǎn)鏈表的頭節(jié)點ListNode start=pre.next;//翻轉(zhuǎn)鏈表,pre.next指向翻轉(zhuǎn)后的鏈表。1->2 變成2->1。 dummy->2->1pre.next=reverse(start);//翻轉(zhuǎn)后頭節(jié)點變到最后。通過.next把斷開的鏈表重新鏈接。start.next=next;//將pre換成下次要翻轉(zhuǎn)的鏈表的頭結(jié)點的上一個節(jié)點。即startpre=start;//翻轉(zhuǎn)結(jié)束,將end置為下次要翻轉(zhuǎn)的鏈表的頭結(jié)點的上一個節(jié)點。即startend=start;}return dummy.next;}//鏈表翻轉(zhuǎn)// 例子: head: 1->2->3->4public ListNode reverse(ListNode head) {//單鏈表為空或只有一個節(jié)點,直接返回原單鏈表if (head == null || head.next == null){return head;}//前一個節(jié)點指針ListNode preNode = null;//當(dāng)前節(jié)點指針ListNode curNode = head;//下一個節(jié)點指針ListNode nextNode = null;while (curNode != null){nextNode = curNode.next;//nextNode 指向下一個節(jié)點,保存當(dāng)前節(jié)點后面的鏈表。curNode.next=preNode;//將當(dāng)前節(jié)點next域指向前一個節(jié)點 null<-1<-2<-3<-4preNode = curNode;//preNode 指針向后移動。preNode指向當(dāng)前節(jié)點。curNode = nextNode;//curNode指針向后移動。下一個節(jié)點變成當(dāng)前節(jié)點}return preNode;}}

?

?

?

總結(jié)

以上是生活随笔為你收集整理的K个一组翻转链表—leetcode25的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。