1. 程式人生 > >連結串列——將單鏈表從m到n的結點位置翻轉

連結串列——將單鏈表從m到n的結點位置翻轉

題目:反轉連結串列從m-n位置的結點

For example:
Given1->2->3->4->5->NULL, m = 2 and n = 4,

return1->4->3->2->5->NULL.

從第二到第四的結點被反轉了。

其中m和n滿足條件:
1 ≤ m ≤ n ≤ length of list.

原地交換結點

public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if(head == null||head.next == null)
            return head;
        if(m == n)
            return head;
        ListNode prefirst=null;//first的前一個結點
        ListNode aftersecond=null;//second的後一個結點
        ListNode first=head;//first引用第m個節點
        ListNode second=null;//secone引用第n個節點
        ListNode p=head;
        for(int i=1;i<=n;i++)
            {
            if(i == m-1)//注意m為1的情況
                {
                prefirst=p;
                first=p.next;
            }
            if(i == n)
                {
                second=p;
                aftersecond=p.next;
            }
            p=p.next;   
        }
        Pair pair=reverse(first,second);
        if(prefirst == null)
            head=pair.head;
        else
            prefirst.next=pair.head;
        pair.rear.next=aftersecond;
        return head;
    }
    //
    public Pair reverse(ListNode head,ListNode rear)
        {
        ListNode pre=head;
        ListNode cur=head.next;
        while(cur!=rear)
            {
            ListNode next=cur.next;
            cur.next=pre;
            pre=cur;
            cur=next;
        }
        cur.next=pre;
        pre=cur;
        rear=head;
        head=pre;
        Pair pair=new Pair(head,rear);
        return pair;
    }
    //定義內部類Pair儲存reverse後的head和rear;
  class Pair
      {
      ListNode head;
      ListNode rear;
      public Pair(ListNode head,ListNode rear)
          {
          this.head=head;
          this.rear=rear;
      }
  }
}


仍是逆序,仍考慮到用輔助空間stack.

將m-n的結點依次入棧,並標記與入棧結點相鄰的前後兩個結點pfirst和psecond

(若m==1,pfirst=null,不管n是否為length of list,對psecond的情況無影響。)

程式碼如下:

import java.util.*;
public class Solution {
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if(head==null)
            return null;
        if(m==n)
            return head;
        Stack<ListNode> stack=new Stack();
        //將m-n的結點入棧,將前後相鄰的兩個結點標記;
        int num=1;
        ListNode pfirst=null;
        ListNode psecond=null;
        ListNode p=head;
        //特殊情況,m==1時,頭結點變更;
          if(m==1)
           pfirst=null;
        for(;num<=n;num++)
            {
            //記錄pfirst;
             if(num<m)
                {
                if(num==m-1)
                    {
                    pfirst=p;
                }
                p=p.next;  
                }    
            else if(num>=m&&num<=n)
                {
                stack.push(p);
                p=p.next;
            }
        }
        //記錄psecond,psecond的一般情況仍適用於n=length of list的特殊情況;
               psecond=p;
        //開始操作連結串列;
        if(pfirst==null)
            {
            head=stack.pop();
            pfirst=head;
        }
        while(!stack.empty())
            {
            pfirst.next=stack.pop();
            pfirst=pfirst.next;
        }
            pfirst.next=psecond;
        return head;
    }
}