链表反转之按段反转重排链表

链表反转请看

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));
      }
}

你可能感兴趣的:(算法)