鏈表反轉之按段反轉重排鏈表

鏈表反轉請看

package com.ym.learn;

import com.google.gson.annotations.Expose;
import com.ym.learn.util.JsonUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * @創建人 yumifen
 * @創建時間 2020/5/18
 * @描述
 *   在做這道題之前,我們不仿先來看看如果從頭部開始組起的話,應該怎麼做呢?
 *   例如:鏈表:1->2->3->4->5->6->7->8->null, K = 3。調整後:3->2->1->6->5->4->7->8->null。其中 7,8不調整,因爲不夠一組。
 **/
public class ListNode1 {

      @Expose
      @Setter
      @Getter
      private  ListNode1  next;
      @Expose
      @Setter
      @Getter
      private  Integer    location;

      public ListNode1(ListNode1 next, Integer location) {
            this.next = next;
            this.location = location;
      }

      public static ListNode1 reverseByKLength(ListNode1 head,Integer kLength) {
           return  reverseByKLength(head,kLength,1);
      }
            /**
             * 第一種方式
             * @param head
             * @param kLength     以幾個分組
             * @param curLength   當前計算到了幾個
             * @return
             */
      private static ListNode1 reverseByKLength(ListNode1 head,Integer kLength,Integer curLength){
            if(kLength == curLength){
                  return head;
            }
            if (head == null||head.getNext()==null)
                  return head;
            curLength++;
            ListNode1 temp = head.getNext();
            ListNode1 newHead = reverseByKLength(temp,kLength,curLength);    //這個是新的排序後的head
            /**分段後重新生成當前節點的下一個**/
            ListNode1 t3 = temp.getNext();
            curLength--;
            ListNode1 res = reverseByKLength(t3,kLength,curLength);
            temp.setNext(head);
            head.setNext(res);
            return newHead;
      }

      public static ListNode1 reverse1(ListNode1 head, int k) {
            ListNode1 current = head;
            ListNode1 next = null;
            ListNode1 prev = null;
            int count = 0;

            /*reverse first k nodes of the linked list */
            while (current != null && count < k) {  //1-》2-》3-》4
                  next  = current.next;   //@1
                  current.next = prev;    //@2
                  prev = current;         //@3
                  current = next;         //@4
                  count++;
            }
            //1 開始  @1 next =2  @2 head.next=current.next=null @3 prev=current=head @4 current=2  結果: prev|head=1 next=2 current=2
            //2 開始  @1 next =3   @2 current.next=1=head  @3 prev=2  @4 current=3  結果:prev=2->1|head next=3 current=3
            //3 開始  @1 next =4    @2 current.next=2   @3prev=3   @4 current=4  結果:prev=3->2-1|head  next=4 current = 4

          /* next is now a pointer to (k+1)th node
             Recursively call for the list starting from current.
             And make rest of the list as next of first node */
            if(next !=  null) {
                  head.next = reverse1(next, k);   //通過以上分析可知連接下一段的重排只需要指向head.next重連就行,連接下一段,
            }
            /* prev is new head of the input list */
            return prev;
      }


      public static void main(String[] args) {
            ListNode1 head = new ListNode1(new ListNode1(new ListNode1(new ListNode1(new ListNode1(null,5),4),3),2),1);
            ListNode1 reverse = reverseByKLength(head,4);
            System.out.println("方法1:"+JsonUtil.object2Json(reverse));

            ListNode1 head1 = new ListNode1(new ListNode1(new ListNode1(new ListNode1(new ListNode1(null,5),4),3),2),1);
            ListNode1 reverse1 = reverse1(head1,4);
            System.out.println("方法2:"+JsonUtil.object2Json(reverse1));
      }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章