SingleLinkList--java

package com.lyzz.singlelinkedList;

public class SingleLinkedList {

	 Node head = null;
	
	/**
	 * 链表中的节点,data代表节点的值,next指向下一节点的引用
	 * @author Lxuex
	 *
	 */
	private class Node{
		private int data;
		private Node next = null;
		
		public Node(int data) {
			this.data = data;
		}
	}
	
	/**
	 * 头插法
	 * @param data
	 */
	public void head_add(int data) {
		Node newNode = new Node(data); 
		if(head == null) {
				head = newNode;
				return ;
			}else {
				newNode.next = head;
				head = newNode;
			}
	}
	
	/**
	 * 尾插法
	 * @param obj
	 * @return
	 */
	public void tail_add(int data) {
		Node newNode = new Node(data); 
		if(head == null) {
				head = newNode;
				return ;
			}else {
				Node temp = head;
				while(temp.next != null) {
					temp = temp.next;
				}
				temp.next = newNode;
			}	
	}
	
	/**
	 * 根据输入数据求下标
	 * @param data
	 * @return
	 */
	 public int get_index(int data) {
		 Node node = head;
		 int index = 0;
		 while(node != null) {
			 if(node.data == data) {
				 System.out.println("数据"+data+"下标是"+index);
				 return index;
			 }
			 node = node.next;
			 index ++;
		 }
		 System.out.println("invalid index");
		 return -1;
	 }
	
	
	/**
	 * 获取链表长度
	 * @return
	 */
	public int get_length() {
		int size = 0;
		Node cur_node = head;
		while(cur_node != null) {
			size ++;
			cur_node = cur_node.next;
		}
		return size;
	}
	
	/**
	 * 打印
	 */
	public void print() {
		Node node = head;
		int size = get_length();
		while(size  >1) {
			System.out.print(node.data+"->");
			node = node.next;
			size --;
			if(size ==1) {
				System.out.print(node.data);
			}
		}
		System.out.println();
	}
	
	/**
	 * 根据节点位置删除节点
	 * @param index
	 * @return
	 */
	public boolean delete_index(int index) {
		//Node head = head;
		if(index < 1 || index > get_length()) {
			return false;
		} 
		
		if(index ==1) {
			head = head.next;
			return true;
		}
		Node pre = head;
		Node cur = head.next;
		int i = 1;
		while(cur != null) {
			if(i == index) {
				pre.next = cur.next;
				return true;
			}
			pre = head.next;
			cur = cur.next;
			i++;
		}
		return true;
	}
	
	/**
	 * 根据节点值删除节点
	 * @param n
	 * @return
	 */
	public boolean delete_data(int n) {
		Node pre = head;
		Node cur = pre.next;
		while(cur != null) {
			if(cur.data == n) {
				pre.next = cur.next;
				return true;
			}
			cur = cur.next;
			pre = pre.next;
		}
		return false;
		
		
	}
	
	public static void main(String[] args) {
		SingleLinkedList list = new SingleLinkedList();
		
		list.head_add(5);
		list.head_add(3);
		list.tail_add(4);
		list.head_add(2);
		list.head_add(1);

		list.print();
		list.ReverseIteratively();
		list.SearchMid();
		System.out.println(list.hasCycle());
		
		list.print();
		
	}
	
	/**
	 * 1.反转链表
	 * @return
	 */
	 public Node ReverseIteratively() {
		    Node pReversedHead = head;
		    Node cur = head;
		    Node pre = null;
		    while (cur != null) {
		      Node pnext = cur.next;
		      if (pnext == null) {
		        pReversedHead = cur;
		      }
		      cur.next = pre;
		      pre = cur;
		      cur = pnext;
		    }
		    this.head = pReversedHead;
		    return this.head;
		  }
	 
	 /**
	  * 2.查找中间值
	  * 采用快慢指针的方式查找单链表的中间节点,快指针一次走两步,慢指针一次走一步,
	  * 当快指针走完时,慢指针刚好到达中间节点。
	  * @return
	  */
	 public Node SearchMid() {
		 Node p = head;
		 Node q = head;
		 while (p != null && p.next != null && p.next.next != null) {
		      p = p.next.next;
		      q = q.next;
		    }
		 System.out.println("Mid:" + q.data);
		    return q;
	 }
	 
	 /**
	  * 3.查找到数第K个元素
	  * 采用两个指针P1,P2,P1先前移K步,然后P1、P2同时移动,当p1移动到尾部时,P2所指位置的元素即倒数第k个元素 
	  * @param k
	  * @return
	  */
	 public Node findElem(int k) {
		 if(k<1 || k> get_length()) {
			 return null;
		 }
		 Node p = head;
		 Node q = head;
		 for(int i =0; i< k; i++) {//前移k步
			 p = p.next;
			 }
			 while(p!=null) {
				 q = q.next;
				 p = p.next;
			 }
			 System.out.println("到数第k个元素是:"+q.data);
			return q;
	 }
	 
	 /**
	  * 4.判断链表是否有环
	  * 快慢指针 快指针一次走两步,慢指针一次走一步,当快和慢相等的时候就是环。
	  * @param head
	  * @return
	  */
	 public boolean hasCycle() {
		 Node slow = head;
		 Node fast = head;
		 while(fast != null) {
			 if (fast.next == null) {
		            return false;
		        }
			 slow = slow.next;
			 fast = fast.next.next;
			 if(fast == slow) {
				 return true;
			 }
		 }
		 return false;
	 }
	 
	 /**
	  * 5.归并排序
	  * @return
	  */
	 public Node merge_sort() {
		return head;	 
	 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值