zoukankan      html  css  js  c++  java
  • 删除链表当中重复的节点

    public class 删除链表中重复的节点
    {
        // 遍历链表让当前节点的前一个节点与后面值大于当前节点相连接
        private static void deleteDuplication(ListNode pHead)
        {
            if (pHead == null)
            {
                return;
            }
            // 要删除的前一个节点
            ListNode preNode = null;
            // 当前节点
            ListNode curNode = pHead;
            while (curNode != null)
            {
                // 当前节点的下一个节点
                ListNode nextNode = curNode.next;
                // 需要进行删除
                if (nextNode != null && curNode.value == nextNode.value)
                {
                    // 当前节点的值
                    int curNodeValue = curNode.value;
                    // 需要进行删除的节点
                    ListNode toBeDeleteNode = curNode;
                    // 遍历找到大于需要删除节点的nextNode
                    while (toBeDeleteNode != null
                            && toBeDeleteNode.value == curNodeValue)
                    {
                        nextNode = toBeDeleteNode.next;
                        toBeDeleteNode = nextNode;
                    }
                    // 当删除的是头节点
                    if (preNode == null)
                    {
                        pHead = nextNode;
                    }
                    else
                    {
                        preNode.next = nextNode;
                    }
                    // 当前节点进行后移
                    curNode = nextNode;
                }
                else
                {// 不需要进行删除
                    preNode = curNode;
                    curNode = nextNode;
                }
            }
        }
    }

  • 相关阅读:
    jQuery $.each用法
    JSON.parse()和JSON.stringify()
    创建对象,初始化对象属性,给节点分派一个合成事件
    javascript 兼容W3c和IE的添加(取消)事件监听方法
    tomcat发布后项目classes下无编译文件
    纯css实现计数器效果
    js点击元素输出对应的index
    鼠标滚轮监听防“抖动”
    原生dom的querySelector、querySelectorAll方法
    spring mvc 通过url传来的参数乱码的解决方法
  • 原文地址:https://www.cnblogs.com/qingtianBKY/p/8184594.html
Copyright © 2011-2022 走看看