Java链表应用

Java链表应用链表类publicclassListNode{intval;ListNodenext=null;ListNode(intval){this.val=val;}}1、返回倒数第k个结点前后指针法需要考虑k大于链表长度的情况publicclassSolution{publicL…

大家好,又见面了,我是你们的朋友全栈君。

链表类

public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}

1、返回倒数第k个结点

前后指针法

需要考虑k大于链表长度的情况

public class Solution {
    public ListNode FindKthToTail(ListNode head,int k) {
        if(k <= 0) {
        	return null;
        }
        ListNode pre = head;
        ListNode cur = head;
        for(int i = 0; i <k -1; i++) {
        	if(cur == null) {
        		return null;//处理k大于链表长度的情况
        	}
        	cur = cur.next;
        }
        if(cur == null) {
        	return null;
        }
        while(cur.next != null) {
        	pre = pre.next;//前后指针
        	cur = cur.next;
        }
        return pre;
    }
}

2、返回两个无环链表的交点

public class Solution2 {
	
	public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
		 int length1 = getListLength(pHead1);
		 int length2 = getListLength(pHead2);
		 int lengthD = length1 - length2;
		 ListNode pLong = pHead1;
		 ListNode pShort = pHead2;
		 if(length1 < length2) {
			 pLong = pHead2;
			 pShort = pHead1;
			 lengthD = -lengthD;
		 }
		 //长链表先走lengthD的长度
		 for(int i = 0; i < lengthD; i++) {
			 pLong = pLong.next;
		 }
		 //两个链表同步走,值相等时停止返回
		 while(pLong != null && pShort != null && pLong != pShort) {
			 pLong = pLong.next;
			 pShort = pShort.next;
		 }
		 return pShort;
    }

	private int getListLength(ListNode pHead1) {
		int length = 0;
		ListNode node = pHead1;
		while(node != null) {
			length++;
			node = node.next;
		}
		return length;
	}
}

3、删除排序链表的重复结点,重复结点不保留 

public class Solution3 {
	public ListNode deleteDuplication(ListNode pHead){
		if(pHead == null || pHead.next == null) {
			return pHead;
		}
		
		if(pHead.val == pHead.next.val) {
			ListNode aft = pHead.next;
			while(aft != null && aft.val == pHead.val) {//跳过所有相同的值,找到第一个不同的值
				aft = aft.next;
			}
			return deleteDuplication(aft);//从第一个不同的值继续递归
		}
		else {
			//当前没有重复元素,就从下一个节点开始
			pHead.next = deleteDuplication(pHead.next);
			return pHead;
		}
    }
}

4、输入一个链表,按链表值从尾到头的顺序返回一个ArrayList

将该链表的值保存在栈中

新建链表,添加栈pop的值

public class Solution4 {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        Stack<Integer> stack = new Stack<>();
        while(listNode != null){
            stack.push(listNode.val);
            listNode = listNode.next;
        }
        ArrayList<Integer> arrayList = new ArrayList<>();
        while(!stack.isEmpty()){
            arrayList.add(stack.pop());
        }
        return arrayList;
    }
}

5、右旋单链表,使每个结点向右移动n个位置,n为非负数

public class Solution5 {
	public ListNode rotateRight(ListNode head, int n) {
	    if(head == null || head.next == null || n == 0){
	        return head;
	    }
	 
	    int len = getLength(head);
	    if(n % len == 0) {
	    	return head;
	    }else {
	    	n %= len;
	    }
	    
	    ListNode mid = head;
	    ListNode pre = head;
	    for(int i = 0; i < len - n; i++){
	        pre = mid;
	        mid = mid.next;
	    }
	    pre.next = null;
	    ListNode newHead = mid;
	    while(mid.next != null){
	        mid = mid.next;
	    }
	    mid.next = head;
	    return newHead;
	}
	private int getLength(ListNode head){
	    ListNode node = head;
	    int len = 0;
	    while(node != null){
	        len++;
	        node = node.next;
	    }
	    return len;
	}
}

6、删除单链表中从后往前数的第n个结点

public class Solution6 {
	public ListNode removeNthFromEnd(ListNode head, int n) {
	    if(head == null){
	        return null;
	    }
	    
	    if(n <= 0){
	        return head;
	    }
	    
	    //前后指针
	    ListNode pre = head;
	    ListNode cur = head;
	    int i = 0;
	    for(; i < n && cur != null; i++){
	        cur = cur.next;
	    }
	    if(i < n) {//处理n大于链表长度的情况
	    	return head;
	    }
	    if(cur == null) {//头删
	    	head = head.next;
	    	return head;
	    }
	    
	    //cur与pre相差n-1个位置
	    //cur = null, pre在n+1处
        while(cur.next != null){
            pre = pre.next;
            cur = cur.next;
        }
        //删除pre
        pre.next = pre.next.next;
        
	    return head;
	}
}

7、反转链表

public class Solution7 {
    public ListNode ReverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
        
        //三个指针遍历
        ListNode pre = null;
        ListNode node = head;
        ListNode aft = head.next;
        while(node != null){
            node.next = pre;
            pre = node;
            node = aft;
            if(aft != null){
                aft = aft.next;
            }
            else{
                aft = null;
            }
        }
        return pre;
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/142640.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • JAVA运用dos命令强制删除文件夹

    JAVA运用dos命令强制删除文件夹在对文件夹进行操作时,如果用file.deleate()方法,有时候恐怕不凑效.用了第三方的FileUtils的forceDeleteFile()还是不凑效,所以,自己就用java写一个基于dos命令的方法来实现强制删除文件夹.这并没有什么难的,只要知道dos命令,在java文件中调用runtime就好办了.在这里为写了三个方法:删除某个文件目录;删除某个文件(不是文件夹)和清空某个文件夹下

    2022年6月8日
    52
  • mmc卡和sd卡区别「建议收藏」

    mmc卡和sd卡区别「建议收藏」转载:https://zhidao.baidu.com/question/296690750.html区别:1、尺寸不同:SD卡的技术是基于MultiMedia卡(MMC)格式上发展而来,大小和MMC卡差不多,尺寸为32mmx24mmx2.1mm。长宽和MMC卡一样,只是比MMC卡厚了0.7mm,以容纳更大容量的存贮单元。2、兼容性不同:SD卡与MMC卡保持着向上兼容,…

    2022年6月11日
    32
  • 无线AP、WiFi、WLAN是什么关系?

    无线AP、WiFi、WLAN是什么关系?最近在看科技论文,有些基础知识不是很清楚,故作了解近年来,无线AP被越来越多的应用于商场、车站、机场等公共场所,已变得与我们的生活息息相关,成为社会发展的一个趋势。但许多人弄不清楚无线AP与WiFi、WLAN之间的关系,以为WiFi就是WLAN,它们之间到底有什么不同?公共WiFi其实是一种无线AP技术今年央视315晚会曝光的公共WiFi安全隐患引起了社会各界的关注,但是有一个细节需要值得注意,那…

    2022年7月11日
    70
  • angular入门教程_初学者织围巾简单教程慢动作

    angular入门教程_初学者织围巾简单教程慢动作课程介绍本课程是一个系列基础教程,目标是带领读者上手实战,课程以新版本Angular的3个核心概念作为主线:组件、路由、模块,加上业务开发过程中必须用到的特性:工具、指令、表单、RxJS、i18n、测试,一共分为9部分,34篇文章。除了组件、路由、模块这3个核心小节具有很强的关联性之外,其它内容都是完全独立的,您可以在用到的时候再翻阅。认真读完这个系列文章之后,您会深入理解…

    2022年10月21日
    0
  • StrictMode 详解「建议收藏」

    StrictMode 详解「建议收藏」StrictMode类是Android2.3(API9)引入的一个工具类,可以用来帮助开发者发现代码中的一些不规范的问题。比如,如果你在UI线程中进行了网络或者磁盘操作,StrictMode就会通过Log(logcat)或者对话框的方式把信息提示给你,因为让你的UI线程处理这里操作会被认为是不规范的做法,可能会让你的应用变得比较卡顿。官网文档:http://developer.an

    2022年5月1日
    31
  • hostapd.conf配置文档「建议收藏」

    hostapd.conf配置文档「建议收藏」#####hostapdconfigurationfile###############################################Emptylinesandlinesstartingwith#areignored#APnetdevicename(without’ap’postfix,i.e.,wlan0useswl

    2022年5月21日
    35

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号