java链表

最近在训练ACM时用C语言编写了很多的链表程序。因为最近在学java,于是便想编写java的链表程序,结果遇到了很多困难。下面的这篇文章很好的概括了整个编写的过程,值得收藏。

       1、单链表的创建和遍历

  2、求单链表中节点的个数

  3、查找单链表中的倒数第k个结点(剑指offer,题15)

  4、查找单链表中的中间结点

  5、合并两个有序的单链表,合并之后的链表依然有序【出现频率高】(剑指offer,题17)

  6、单链表的反转【出现频率最高】(剑指offer,题16)

       7、删除单链表中的重复元素

  8、从尾到头打印单链表(剑指offer,题5)

  9、判断单链表是否有环

  10、取出有环链表中,环的长度

  11、单链表中,取出环的起始点(剑指offer,题56)。本题需利用上面的第8题和第9题。

  12、判断两个单链表相交的第一个交点(剑指offer,题37)

1、单链表的创建和遍历:

public class LinkList{

	public Node head;

	public Node current;
	
	//方法:向链表中添加数据
	public void add(int data){

		//判断链表为空的时候
		if(head == null) {
			//如果头结点为空,说明这个链表还没有创建,那就把新的结点赋给头结点

			head= new Node(data);

			current = head;
		}
		
		else{
			//创建新的结点,放在当前节点的后面(把新的结点合链表进行关联)

			current.next = new Node(data);

			//把链表的当前索引向后移动一位

			current = current.next; //此步操作完成之后,current结点指向新添加的那个结点

		}

}

 

//方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进行遍历

	public void print(Node node) {

		if(node == null){
			
			return;
		
		}

 

		current = node;

		while(current != null) {

			System.out.println(current.data);	

			current = current.next;

		}

	}

 

	class Node {

		//注:此处的两个成员变量权限不能为private,因为private的权限是仅对本类访问。

		int data;//数据域

		Node next;//指针域

 

		public Node(int data) {
		
			this.data = data;
		
		}
		
	}
		public static void main(String[] args) {
		
			LinkList list = new LinkList();//向LinkList中添加数据
		
				for(int i = 0; i < 10; i++) {
				
					list.add(i);
		        
				}
		                
		        list.print(list.head);//从head节点开始遍历输出
	
	}
	
}

上方代码中,这里面的Node节点采用的是内部类来表示(60行)。使用内部类的最大好处是可以和外部类进行私有操作的互相访问。

注:内部类访问的特点是:内部类可以直接访问外部类的成员,包括私有;外部类要访问内部类的成员,必须先创建对象。

为了方便添加和遍历的操作,在LinkList类中添加一个成员变量current,用来表示当前节点的索引(05行)。

这里面的遍历链表的方法(36行)中,参数node表示从node节点开始遍历,不一定要从head节点遍历。

 

2、求单链表中节点的个数:

注意检查链表是否为空。时间复杂度为O(n)。这个比较简单。

核心代码:

//方法:获取单链表的长度

	public int getLength(Node head) {

		if(head == null){
	
			return 0;
	
		}


		int length = 0;

		Node current = head;

		while(current != null){

			length++;

			current = current.next;

		}


		return length;

	}

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

3.1  普通思路:

先将整个链表从头到尾遍历一次,计算出链表的长度size,得到链表的长度之后,就好办了,直接输出第(size-k)个节点就可以了(注意链表为空,k为0,k为1,k大于链表中节点个数时的情况

)。时间复杂度为O(n),大概思路如下:

	public int findLastNode(int index){ //index代表的是倒数第index的那个结点


		//第一次遍历,得到链表的长度size

		if(head == null){

			return-1;

		}

		int size=0;

		current = head;

		while(current!= null){

			size++;

			current = current.next;

		}



		//第二次遍历,输出倒数第index个结点的数据

		current = head;

		for(int i = 0;i < size - index; i++) {

			current = current.next;

		}



		return current.data;

	}

如果面试官不允许你遍历链表的长度,该怎么做呢?接下来就是。

 3.2  改进思路:(这种思路在其他题目中也有应用)

     这里需要声明两个指针:即两个结点型的变量first和second,首先让first和second都指向第一个结点,然后让second结点往后挪k-1个位置,此时first和second就间隔了k-1个位置,然后整体向后移动这两个节点,直到second节点走到最后一个结点的时候,此时first节点所指向的位置就是倒数第k个节点的位置。时间复杂度为O(n)

代码实现:(初版)

	public Node findLastNode(Node head, int index){

		if(head == null){

			return null;

		}

	 

		Node first = head;

		Node second = head;

		//让second结点往后挪index个位置

		for(int i= 0;i < index; i++) {

			second = second.next;

		}

	 

		//让first和second结点整体向后移动,直到second结点为Null

		while(second != null){

			first = first.next;

			second = second.next;

		}

	 

		//当second结点为空的时候,此时first指向的结点就是我们要找的结点

		return first;

	}

代码实现:(最终版)(考虑k大于链表中结点个数时的情况时,抛出异常)

上面的代码中,看似已经实现了功能,其实还不够健壮:

  要注意k等于0的情况;

  如果k大于链表中节点个数时,就会报空指针异常,所以这里需要做一下判断。

核心代码如下:

	public Node findLastNode(Node head, int k){  
		  
	    if(k == 0|| head == null) {  
	  
	        return null;  
	  
	    }  
	  
	  
	  
	    Node first = head;  
	  
	    Node second = head;  
	  
	  
	  
	    //让second结点往后挪k-1个位置  
	  
	    for(int i = 0; i < k - 1; i++) {  
	  
	        System.out.println("i的值是"+ i);  
	  
	        second = second.next;  
	  
	        if(second == null) { //说明k的值已经大于链表的长度了  
	  
	            //throw  
	            new NullPointerException("链表的长度小于" + k); //我们自己抛出异常,给用户以提示  
	  
	            return null;  
	  
	        }  
	  
	    }  
	    
	    //让first结点向后挪k-1个位置
	    for(int i=0; i < k-1;i++) {
	    	
	    	first=first.next;
	    
	    }
	    
	    return first;//返回此时first的值即为要找点的值

	}

4、查找单链表中的中间结点:

同样,面试官不允许你算出链表的长度,该怎么做呢?

思路:

    和上面的第2节一样,也是设置两个指针first和second,只不过这里是,两个指针同时向前走,second指针每次走两步,first指针每次走一步,直到second指针走到最后一个结点时,此时first指针所指的结点就是中间结点。注意链表为空,链表结点个数为1和2的情况。时间复杂度为O(n)。

代码实现:

	//方法:查找链表的中间结点

	public Node findMidNode(Node head) {
	 

		if(head == null) {

			return null;

		}

		Node first = head;

		Node second = head;

		//每次移动时,让second结点移动两位,first结点移动一位

		while(second != null&& second.next != null) {

			first = first.next;

			second = second.next.next;

		}

		//直到second结点移动到null时,此时first指针指向的位置就是中间结点的位置

		return first;

	}

上方代码中,当n为偶数时,得到的中间结点是第n/2 + 1个结点。比如链表有6个节点时,得到的是第4个节点。

 

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

    这道题经常被各公司考察。

例如:

链表1:

  1->2->3->4

链表2:

  2->3->4->5

合并后:

  1->2->2->3->3->4->4->5

解题思路:

  挨着比较链表1和链表2。

  这个类似于归并排序。尤其要注意两个链表都为空、和其中一个为空的情况。只需要O (1) 的空间。时间复杂度为O (max(len1,len2))

代码实现:

//两个参数代表的是两个链表的头结点

	public Node mergeLinkList(Node head1, Node head2) {
	 
		if(head1 == null&& head2 == null) { //如果两个链表都为空

			return null;

		}

		if(head1 == null) {

			return head2;

		}

		if(head2 == null) {

			return head1;

		}

	 

		Node head; //新链表的头结点

		Node current; //current结点指向新链表

		// 一开始,我们让current结点指向head1和head2中较小的数据,得到head结点

		if(head1.data < head2.data) {

			head = head1;

			current = head1;

			head1 = head1.next;

		}
		
		else{

			head = head2;

			current = head2;

			head2 = head2.next;

		}

	 

		while(head1 != null && head2 != null) {

			if(head1.data < head2.data) {

				current.next = head1; //新链表中,current指针的下一个结点对应较小的那个数据

				current = current.next; //current指针下移

				head1 = head1.next;

			}
			
			else{

				current.next = head2;

				current = current.next;

				head2 = head2.next;

			}

		}

	 

	 //合并剩余的元素

		if(head1 != null) { //说明链表2遍历完了,是空的

			current.next = head1;

		}

	 

		if(head2 != null) { //说明链表1遍历完了,是空的

			current.next = head2;

		}

	 

		return head;

	}
 

代码测试:
public static void main(String[] args) {

		LinkList list1 = newLinkList();

		LinkList list2 = newLinkList();

		//向LinkList中添加数据

		for(int i = 0; i < 4; i++) {

			list1.add(i);

		}

			 

		for(int i = 3; i < 8; i++) {

			list2.add(i);

		}

			 
		LinkList list3 = newLinkList();

		list3.head = list3.mergeLinkList(list1.head, list2.head); //将list1和list2合并,存放到list3中

		list3.print(list3.head);// 从head节点开始遍历输出

	}

上方代码中用到的add方法和print方法和第1小节中是一致的。


6、单链表的反转:

	 public static Node reverse(Node head)  
	    {  
	        if (null == head)  
	        {  
	           return null; 
	        }  
	        Node pre = head;  
	        Node cur = head.next;  
	        Node next;  
	        while (null != cur)  
	        {  
	            next = cur.next;  
	            cur.next=pre;  
	            pre = cur;  
	            cur = next;  
	        }  
	  
	        // 将原链表的头节点的下一个节点置为null,再将反转后的头节点赋给head  
	        head.next=null;  
	        return pre;
	    }  

}

7、删除单链表中的重复元素:

	public static int deleteDupsNew(Node head) {  
	    if (head == null) return 0;  
	    Node current = head;
	    int x=0;
	    while(current != null){  
	        Node runner = current;  
	        while (runner.next != null){  
	            if(runner.next.data == current.data){  
	                runner.next = runner.next.next;
	                x++;
	            }  
	            else{  
	                runner = runner.next;  
	            }  
	        }  
	        current = current.next;  
	    }
	    return x;
	}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值