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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • fedora14安装教程_fedora安装中文

    fedora14安装教程_fedora安装中文 Dropbox是非常好用到文件备份与同步工具,而且已经从以前的只支持Windows到现在的多系统支持,但是在Fedora下Dropbox只提供对Fedora10的RPM安装包,这让使用最新版本的Fedora的人来说是非常不爽的。今天我们就通过下载Dropbox的源码在Fedora12上通过编译安装来使用。#1首先在Dropbox官方网站上下载程序的源码。源码下载页

    2022年9月20日
    4
  • pycharm 注释多行代码[通俗易懂]

    pycharm 注释多行代码[通俗易懂]pycharm注释多行代码:选中要注释的代码块,按ctrl+/

    2022年8月28日
    1
  • 微信JS-SDK实现自定义分享功能,分享给朋友,分享到朋友圈「建议收藏」

    微信JS-SDK实现自定义分享功能,分享给朋友,分享到朋友圈「建议收藏」微信JS-SDK实现自定义分享功能,分享给朋友,分享到朋友圈导语:微信分享在手机右上角的三个点一键分享就ok了,那么对于分享到朋友圈,分享给朋友是怎么实现的呢?对于那种活动分享送流量是怎么定位分享者的呢?而想要将文章发送给朋友又是怎么获取到的朋友列表的呢?微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包。JSSDK使用步骤1、绑定域

    2022年5月13日
    38
  • mysql executereader_“c#”中“ExecuteReader”是什么意思?「建议收藏」

    mysql executereader_“c#”中“ExecuteReader”是什么意思?「建议收藏」1、MSDN上说:SendstheCommandTexttotheConnectionandbuildsaSqlDataReader.简单说,就是SqlCommand对象的方法,执行返回数据的Select语句。它的执行方法有两个:第一,ExecuteReader():针对Connection执行CommandText,并返回DbDataReader。第二,ExecuteReade…

    2022年6月20日
    24
  • WIN10下 Tomcat安装及配置教程「建议收藏」

    WIN10下 Tomcat安装及配置教程「建议收藏」目录工具/原料方法/步骤注意事项工具/原料1,JDK:版本为jdk1.8我的下载文件里有,解压缩版的2,tomcat:版本为apache-tomcat-8.0.53-windows-x64.zip下载地址http://tomcat.apache.org/3,windows10,64bit方法/步骤一、安装JDK和Tomcat1,安装JDK:解压即可,…

    2022年5月12日
    45
  • SAE J1939协议学习笔记

    SAE J1939协议学习笔记引用百度百科:SAEJ1939(以下简称J1939)是美国汽车工程协会(SAE)的推荐标准,用于为中重型道路车辆上电子部件间的通讯提供标准的体系结构。它由“卡车与大型客车电气与电子委员会”(Track&BusElectrical&ElectronicsCommittee)下属的“卡车与大型客车控制和通讯网络附属委员会”(Track&BusContro…

    2022年5月3日
    40

发表回复

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

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