反转部分单项链表之Java实现


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

}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值