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

  • 相关阅读:
    NetBeans + Struts + Hibernate 入门教程
    The Perils of JavaSchools
    Java 2 EE 开发初体验
    五周年记
    MySQL Workbench 介绍
    JAVA语言学校的危险性
    娱乐无处不在 Sun工程师惊天发现:大吼可致硬盘潜伏期激增
    争什么?
    spring cloud使用zuul实现反向代理和负载均衡
    spring cloud 网管篇zuul
  • 原文地址:https://www.cnblogs.com/qingtianBKY/p/8184594.html
Copyright © 2011-2022 走看看