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;
                }
            }
        }
    }

  • 相关阅读:
    MySQL Date函数的正确用法
    CentOS tree命令详解
    VMware虚拟机的CentOS无法上网的解决方法
    CentOS安装emacs24.2命令
    【复杂】CentOS 6.4下PXE+Kickstart无人值守安装操作系统
    CentOS定位、查找文件的命令
    CentOS查找目录或文件
    CentOS安装Emacs文本编辑器
    centos6.5的软件安装,用户管理等
    CentOS安装OpenOffice
  • 原文地址:https://www.cnblogs.com/qingtianBKY/p/8184594.html
Copyright © 2011-2022 走看看