package com.zfq.dataStructure01;
public class ReversePart {
public static Node reversePart(Node head,int f,int t)
{
if(f>t||f<1)
return head;
Node pre1=head;
Node pre2=head;
//找到要删除节点的前一个节点
while(--f!=1)
pre1=pre1.next;
//找到要删除节点的后一个节点
while((t--)!=0)
pre2=pre2.next;
Node pre=pre2;
Node next=null;
Node pp=pre1.next;
while(pp!=pre2)
{
next=pp.next;
pp.next=pre;
pre=pp;
pp=next;
}
pre1.next=pre;
return head;
}
}
//创建节点
package com.zfq.dataStructure01;
public class Node {
public int data;
public Node next;
public Node(int data)
{
this.data=data;
}
}
//创建链表
public class CreateQueue {Node a=new Node(1);;Node b=new Node(2);;Node c=new Node(3);;Node d=new Node(4);;Node e=new Node(5);;Node f=new Node(6);;public CreateQueue(){a.next=b;b.next=c;c.next=d;d.next=e;e.next=f;f.next=null;}}
public class Test {
public static void main(String[] args) {
CreateQueue cq=new CreateQueue();
Node cur=cq.a;
System.out.println("之前:");
while(cur!=null)
{
System.out.print(cur.data+" ");
cur=cur.next;
}
Node head=ReversePart.reversePart(cq.a,2,4);
System.out.println();
System.out.println("之后:");
cur=head;
while(cur!=null)
{
System.out.print(cur.data+" ");
cur=cur.next;
}
}
}