1.求单链表节点个数
思路:遍历单链表,每次遍历一个节点计数器加1即可。
public int getlenth()
{
HeroNode temp=head.next;
if(head.next==null)
return 0;
else
{
int len=0;
while(temp!=null)
{
len++;
temp=temp.next;
}
return len;
}
}
2.查找单链表的倒数第k个节点
思路:用双指针指向链表tail和target,先将tail指针移动k-1个位置,再将target指针和tail指针同时移动,直到tail指针指向链表尾部才结束,这时tail指针和target指针相差k-1位,即target是倒数第k个节点
public void getfinalkthnode(int k)
{
if(head.next==null || k>getlenth())
System.out.print("链表空或者k值大于链表长度\n");
else
{
HeroNode tail=head;
HeroNode target=head;
for(int i=1;i<k;i++)
{
tail=tail.next;
}
tail=tail.next;
target=head.next;
while(tail.next!=null) //遍历到最后一个停止
{
tail=tail.next;
target=target.next;
}
System.out.println(target);
return ;
}
}
3.单链表反转
思路:第一种就是用头插法进行的单链表逆转操作,但是这种操作就是破坏了之前的单链表结构,代价就比较大。首先主要创建一个新的节点当做头结点,然后在原来的链表中设计两个指针,一个cur指针指向当前待插入节点,之后的next指针指向待操作节点的下一个节点,因为待操作节点经过操作后指针已经乱套了,要想遍历就必须要一个next指针可以表示出下一个节点的位置。
//************************************反转链表,头插法可以,这种方式就破坏了单链表的结构************************
public void Reverse()
{
//如果链表为空或者链表只有一个节点,不需要反转
if(head.next==null || head.next.next==null)
return ;
HeroNode newhead=new HeroNode(0,"","");//不能赋值,会打乱原来的链表,只能new一个新的链表
HeroNode cur=head.next; //当前节点取出
HeroNode next=null; //当前节点的下一个节点用来移动
while(cur!=null)
{
next=cur.next; //保存后续指针
//头插
cur.next=newhead.next;
newhead.next=cur;
cur=next; //这一步如果直接用cur=cur.next,cur操作过后已经改变,这样做链表就断了
}
head.next=newhead.next;
HeroNode temp=head.next;
System.out.print("逆转后的链表为\n");
while(temp!=null)
{
System.out.println(temp);
temp=temp.next;
}
}
这块自己写的代码陷入误区以为将cur赋值给temp之后对temp操作不影响cur,其实是影响的,因为这两个都指向同一个数据,temp操作后,之前的cur.next已经变化,再想找之后的节点是不可能了。一定要定义一个节点来存储下一个节点所在位置。
// //HeroNode newhead=head; //不能赋值,会打乱原来的链表,只能new一个新的链表
// HeroNode newhead=new HeroNode(0,"","");
// HeroNode temp; //插入节点信息保存
// HeroNode cur=head.next; //移动位置用的
// while(cur!=null)
// {
// temp=cur; //这里temp=cur,不是一个临时变量,temp指向了这个链表,而之后的temp操作会影响到cur,这时再用cur=cur.next指向就会出错
// System.out.println(temp);
//所以只有保存下一个节点的指针,保证在当前节点操作不会使得之后的节点找不到
// temp.next=newhead.next;
// newhead.next=temp;
// System.out.print(newhead.next.heronum);
// cur=cur.next;
// }
// temp=newhead.next;
// System.out.print("逆转后的+链表为\n");
// //System.out.println(temp);
// while(temp!=null)
// {
// System.out.println(temp);
// temp=temp.next;
// }
这一种是用栈来实现的,利用的栈的先进后出特性容易就实现了逆序,而且不破坏原来的链表结构。就是直接调用的java库里的链表结构,以后要自己实现栈的相关操作。
//***************************************使用栈逆序打印***********************************
public void reversebystack()
{
if(head.next==null)
return;
//创建栈
Stack<HeroNode> stack = new Stack<HeroNode>();
HeroNode temp = head.next;
while(temp!=null)
{
stack.add(temp);
temp=temp.next;
}
System.out.print("逆序打印单链表为\n");
while(stack.size()>0)
{
System.out.println(stack.pop());
}
}
4.合并两个有序的单链表使得合并后的单链表依然有序。
思路:新建C头结点,一个cur指针,首先A,B两个链表都是有序的。这样的话就是逐个元素比较A,B链表节点的大小。如果A中的节点的值比较大,那么就让cur接B节点,B中的节点向后移动,A中节点不动,继续让B的节点和A的节点比较,如果A的节点小于B的节点,那么就让cur接A的节点,A的节点向后移动继续和B的节点比较。知道A,B两个链表有一个为空,比较结束,将剩下的一个不为空的节点直接接上剩余部分即可。
//***********************************合并两个单链表使得依然有序******************//
public void contate(HeroNode headA,HeroNode headB)
{
HeroNode headC=new HeroNode(0,"","");
HeroNode curA=headA.next;
HeroNode curB=headB.next;
HeroNode cur=headC;
HeroNode temp;
while(curA!=null && curB!=null)
{
if(curA.heronum>curB.heronum) //如果A比B大,那么应该接B,否则接A
{
//System.out.print("接A\n");
cur.next=curB;
curB=curB.next;
}
else //接A
{
//System.out.print("接B\n");
cur.next=curA;
curA=curA.next;
}
cur=cur.next;
}
if(curA!=null)
cur.next=curA;
else
cur.next=curB;
temp=headC.next;
while(temp!=null)
{
System.out.println(temp);
temp=temp.next;
}
}