链表的各种基本操作

本文详细介绍了链表的基本实现,包括如何反转链表、判断链表是否存在循环、删除倒数第k个元素的两种方法以及如何判断链表是否为回文。内容源于个人学习笔记,欢迎指正。
摘要由CSDN通过智能技术生成

来源:https://loliwithpick.github.io/deny.github.io/2019/03/15/linkedList/

链表的基本实现


class LinkList{
	//存放头结点
    Node head;
    //记录数据长度
    int size;

    LinkList(){
    	head = new Node();
    	size = 0;
    }

    class Node{
    	public int value = -1;
    	public Node next = null;

    	Node(int value) {
        this.value = value;
        }

      	public Node(){}
    }

    /**
     * 在链表头添加节点
     * @param value
     */
    public void addFirst(int value) {
      	final Node node = new Node(value);
     	final Node nextN = head.next;
      	node.next = nextN;
     	head.next = node;
     	size++;
    }

    /**
     * 在链尾添加节点
     * @param value
     */
    public void addLast(int value) {
      	final Node node = new Node(value);
      	Node cur = head;
      	while (cur.next != null)
        	cur = cur.next;
      	cur.next = node;
      	size++;
    }

    /**
     * 指定位置添加节点
     * @param value
     * @param index
     */
    public void add(int value, int index) {
      	if (index > size)
        	return;
      	Node pre = null, cur = head;
      	final Node node = new Node(value);
      	for (int i = 0; i < index; i++) {
        	pre = cur;
        	cur = cur.next;
      	}
      	pre.next = node;
      	node.next = cur;
    }

    public int getSize() {
      	return size;
    }

    /**
     * 删除链头节点并返回节点值
     * @return
     */
    public int removeFirst() {
      	if (size == 0)
        	return -1;
      	final Node node = head.next;
      	final Node nextN = node.next;
      	head.next = nextN;
      	size--;
      	return node.value;
    }

    /**
     * 删除链尾节点并返回节点值
     * @return
     */
    public int removeLast(){
      	if (size == 0)
        	return -1;
      	Node cur = head;
      	while (cur.next != null && cur.next.next != null) {
        	cur = cur.next;
      	}
      	final Node node = cur.next;
      	cur.next = null;
      	size--;
      	return node.value;
    }

反转链表


    /**
     * 反转链表
     */
    public void reverse(){
      	if (size == 0)
        	return;
      	Node pre = null, cur = head.next;
      	while (cur != null) {
        	Node tmp = cur.next;
        	cur.next = pre;
        	pre = cur;
        	cur = tmp;
      	}
      	head.next = pre;
    }

判断链表是否存在循环

    /**
     * 判断是否存在循环
     */
    public boolean loopCheck(){
      	if (size == 0)
        	return false;
      	Node first = head.next;
      	Node second = first.next;
      	while (second != null && second.next != null){
        	if (first == second)
          	return true;
        	first = first.next;
        	second = second.next.next;
      	}
      	return false;
    }

删除倒数第 k 个元素的两种方法

    /**
     * 删除倒数第 k 个数(方法一)
     * @param k
     * @return
     */
    public int deleteDESC(int k) {
      	if (size == 0 || k > size)
        	return -1;
      	Node cur = head;
      	for (int i = 0; i < (size - k); i++) {
        	cur = cur.next;
      	}
      	final Node tmp = cur.next;
      	cur.next = tmp.next;
      	return tmp.value;
    }

    /**
     * 删除倒数第 k 个数(方法二)
     * @param k
     * @return
     */
    public int deleteLastKth(int k) {
      	if (size == 0 || k > size)
        	return -1;
      	Node first = head.next;
      	while (k > 1) {
        	first = first.next;
        	k--;
      	}
      	Node second = head.next, pre = head;
      	while (first.next != null) {
        	first = first.next;
        	pre = second;
        	second = second.next;
      	}
      	pre.next = second.next;
      	return second.value;
    }

回文链表的判断

    /**
     * 判断是否回文链表
     * @return
     */
    public boolean palindrome(){
      	if (size <= 1)
        	return false;
      	Node first = head.next, second = head.next.next, left, right;
      	while (second.next != null && second.next.next != null) {
        	first = first.next;
        	second = second.next.next;
      	}
      	if (second.next != null) {
        	right = first.next.next;
        	left = reverse(first);
      	} else {
        	right = first.next;
        	left = reverse(first);
      	}
      	return ToF(left, right);
    }

    /**
     * 反转以参数节点为尾的链表
     * @param node
     * @return
     */
    public Node reverse(Node node) {
      	Node pre = null, cur = head.next;
      	while (cur != node) {
        	Node tmp = cur.next;
        	cur.next = pre;
        	pre = cur;
        	cur = tmp;
      	}
      	cur.next = pre;
      	return cur;
    }

    /**
     * 判断两个链表所有的节点值是否相等
     * @param left
     * @param right
     * @return
     */
    public boolean ToF(Node left, Node right) {
      	while (left != null && right != null && left.value == right.value) {
        	left = left.next;
        	right = right.next;
      	}
      	if (left == null && right == null)
        	return true;
      	else
        	return false;
    }

    /**
     * 输出所有节点值
     */
    public void print() {
      	Node cur = head.next;
      	while (cur != null) {
        	System.out.print(cur.value+" ");
        	cur = cur.next;
      	}
    }
  }

以上均为本人学习时的笔录总结,若有错误之处,请多多指教。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值