<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)25:K 個一組翻轉(zhuǎn)鏈表

          共 1911字,需瀏覽 4分鐘

           ·

          2020-09-02 00:05

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !


          今天和大家聊的問題叫做K 個一組翻轉(zhuǎn)鏈表,我們先來看題面:

          https://leetcode-cn.com/problems/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.

          k is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.


          題意


          給你一個鏈表,每 k 個節(jié)點一組進行翻轉(zhuǎn),請你返回翻轉(zhuǎn)后的鏈表。
          k 是一個正整數(shù),它的值小于或等于鏈表的長度。
          如果節(jié)點總數(shù)不是 k 的整數(shù)倍,那么請將最后剩余的節(jié)點保持原有順序。

          樣例


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

          當?k?= 2 時,應當返回: 2->1->4->3->5

          當?k?= 3 時,應當返回: 3->2->1->4->5

          題解

          每k個節(jié)點進行一次反轉(zhuǎn),用兩個指針記錄k個區(qū)間的首尾節(jié)點,寫一個reverse函數(shù)反轉(zhuǎn)該局部內(nèi)的節(jié)點指向,接著繼續(xù)向后走,不停的取k個進行反轉(zhuǎn),如果不夠k個就返回。具體代碼如下:

          import java.util.Scanner;

          public?class?Main?{
          ????//定義Node節(jié)點
          ????static?class?ListNode?{
          ????????public?int?val;
          ????????public?ListNode next = null;

          ????????public?ListNode(int?val) {
          ????????????this.val = val;
          ????????}
          ????}

          ????public?static?void?main(String[] args) {
          ????????//1.獲取輸入信息
          ????????Scanner scanner = new?Scanner(System.in);
          ????????String string?= scanner.nextLine();
          ????????int?k = scanner.nextInt();
          ????????String[] strings = string.split(" ");
          ????????//2.創(chuàng)建頭結(jié)點
          ????????ListNode head = new?ListNode(0);
          ????????ListNode tail = head;
          ????????//3.將輸入的字符串變?yōu)殒湵砉?jié)點
          ????????for?(String str : strings) {
          ????????????ListNode newNode = new?ListNode(Integer.valueOf(str));
          ????????????tail.next = newNode;
          ????????????tail = tail.next;
          ????????}
          ????????head = head.next;
          ????????//每k個反轉(zhuǎn)鏈表
          ????????ListNode node = reverseGroup(head, k);
          ????????while(node!=null){
          ????????????System.out.print(node.val+" ");
          ????????????node = node.next;
          ????????}
          ????}

          ????//不停地取k個進行翻轉(zhuǎn),如果不夠k個,就直接返回,結(jié)束
          ????public?static?ListNode reverseGroup(ListNode head, int?k) {
          ????????if?(head == null?|| head.next == null?|| k <= 1)
          ????????????return?head;
          ????????ListNode currentNode = head;
          ????????//獲取k個元素的首尾節(jié)點
          ????????for?(int?count = 1; count < k; count++) {
          ????????????currentNode = currentNode.next;
          ????????????//不夠K個則返回
          ????????????if(currentNode==null)
          ????????????????return?head;
          ????????}
          ????????ListNode next = currentNode.next;
          ????????//對局部鏈表進行反轉(zhuǎn)
          ????????reverse(head,currentNode);
          ????????head.next=reverseGroup(next,k);
          ????????return?currentNode;
          ????}

          ????//寫一個頭尾節(jié)點反轉(zhuǎn)的局部函數(shù)
          ????public?static?ListNode reverse(ListNode head, ListNode tail) {
          ????????if?(head == null?|| head.next == null)
          ????????????return?head;
          ????????ListNode pre = null;
          ????????ListNode next = null;
          ????????while?(pre != tail) {
          ????????????next = head.next;
          ????????????head.next = pre;
          ????????????pre = head;
          ????????????head = next;
          ????????}
          ????????return?pre;
          ????}

          }

          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力。

          上期推文:


          LeetCode1-20題匯總,速度收藏!
          LeetCode刷題實戰(zhàn)21:合并兩個有序鏈表
          LeetCode刷題實戰(zhàn)23:合并K個升序鏈表
          LeetCode刷題實戰(zhàn)24:兩兩交換鏈表中的節(jié)點


          瀏覽 176
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  色色操美女 | 久久色国产精品 | 亚洲女人操逼视频 | 天天澡日日久综合 | 国产精品久久久久久久久午夜福利 |