学习笔记-单链表操作

求单链表中有效节点的个数

/**
*
* @param head 链表的头节点
* @return 返回的就是有效节点的个数
*/
public static int getLength(HeroNode head) {
	if(head.next == null) { //空链表
		return 0;
	}
	int length = 0;
	//定义一个辅助的变量, 这里我们没有统计头节点
	HeroNode cur = head.next;
	while(cur != null) {
		length++;
		cur = cur.next; //遍历
	}
	return length;
}

查找单链表中的倒数第 k 个结点

//思路
//1. 编写一个方法,接收 head 节点,同时接收一个 index
//2. index 表示是倒数第 index 个节点
//3. 先把链表从头到尾遍历,得到链表的总的长度 getLength
//4. 得到 size 后,我们从链表的第一个开始遍历 (size-index)个,就可以得到
//5. 如果找到了,则返回该节点,否则返回 nulll
public static HeroNode findLastIndexNode(HeroNode head, int index) {
	//判断如果链表为空,返回 null
	if(head.next == null) {
		return null;//没有找到
	}	
	//第一个遍历得到链表的长度(节点个数)
	int size = getLength(head);
	//第二次遍历 size-index 位置,就是我们倒数的第 K 个节点
	//先做一个 index 的校验
	if(index <=0 || index > size) {
		return null;
	}
	//定义给辅助变量, for 循环定位到倒数的 index
	HeroNode cur = head.next; //3 // 3 - 1 = 2
	for(int i =0; i< size - index; i++) {
		cur = cur.next;
	}
	return cur;
}

单链表的反转

在这里插入图片描述

//将单链表反转
public static void reversetList(HeroNode head) {
	//如果当前链表为空,或者只有一个节点,无需反转,直接返回
	if(head.next == null || head.next.next == null) {
		return ;
	}
	//定义一个辅助的指针(变量),帮助我们遍历原来的链表
	HeroNode cur = head.next;
	HeroNode next = null;// 指向当前节点[cur]的下一个节点
	HeroNode reverseHead = new HeroNode(0, "", "");
	//遍历原来的链表,每遍历一个节点,就将其取出,并放在新的链表 reverseHead 的最前端
	//动脑筋
	while(cur != null) {
		next = cur.next;//先暂时保存当前节点的下一个节点,因为后面需要使用
		cur.next = reverseHead.next;//将 cur 的下一个节点指向新的链表的最前端
		reverseHead.next = cur; //将 cur 连接到新的链表上
		cur = next;//让 cur 后移
	}
	//将 head.next 指向 reverseHead.next , 实现单链表的反转
	head.next = reverseHead.next;
}

从尾到头打印单链表

//可以利用栈这个数据结构,将各个节点压入到栈中,然后利用栈的先进后出的特点,就实现了逆序打印的效public static void reversePrint(HeroNode head) {
	if(head.next == null) {
		return;//空链表,不能打印
	}
	//创建要给一个栈,将各个节点压入栈
	Stack<HeroNode> stack = new Stack<HeroNode>();
	HeroNode cur = head.next;
	//将链表的所有节点压入栈
	while(cur != null) {
		stack.push(cur);
		cur = cur.next; //cur 后移,这样就可以压入下一个节点
	}
	//将栈中的节点进行打印,pop 出栈
	while (stack.size() > 0) {
		System.out.println(stack.pop()); //stack 的特点是先进后出
	}
}

合并两个有序的单链表,合并之后的链表依然有序

public static StudentNode concat(StudentNode head1,StudentNode head2){
        if(head1.next==null &head2.next==null){
            return null;
        }else if(head1.next==null){
            return head2;
        }else if(head2.next==null){
            return head1;
        }else {
            StudentNode newhead=new StudentNode(0,"");
            newhead.next=head1.next;
            StudentNode temp=head2.next;
            //用来保存temp后边的数据
            StudentNode n=null;
            while (true){
                if(temp==null){
                    break;
                }else {
                    n=temp.next;
                    StudentNode cur=newhead;
                    while (true){
                        if(cur.next==null){
                            break;
                        }else if(cur.next.no>temp.no){
                            break;
                        }else if(cur.next.no==temp.no){
                            throw new RuntimeException("编号重复");
                        }
                        cur=cur.next;
                    }
                    temp.next=cur.next;
                    cur.next=temp;
                }
                temp=n;
            }
            return newhead;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值