《剑指offer---面试题18:删除链表的节点》

《剑指offer—面试题18:删除链表的节点》
注明:仅个人学习笔记
package com.chapter3.code;
/**
*
*
* SingleLinkedList:单链表操作
* Node:链表节点
*
*
*/
public class DeleteRepeatData
{

public static Node deleteDuplication(Node pHead)
{

    // 如果链表为空,或者只有一个结点,直接返回
    if (pHead == null)
    {
        System.out.println("请输出合法链表");
        return pHead;
    }

    if (pHead.next == null)
    {
        System.out.println("当前链表节点唯,无法重复元素删除");
        return pHead;
    }

    Node pPreNode = null;// 当前节点的前一个节点
    Node pNode = pHead;// 当前节点

    // 如果链表结点大于等于2,则比较当前结点和下一个结点是否相同,
    // 如果相同,则删除当前结点和下一个结点,并比较下下个结点是否和当前结点相同,如果相同继续删除,直至与当前结点不同。
    while (pNode != null)
    {
        Node pNext = pNode.next;// 当前节点的下一节点
        boolean needDelete = false;// 是否需要删除

        // 当,当前节点的下一节点不为空,且当前节点的值和当前节点的下一节点的值相等时,节点需要删除
        if (pNext != null && pNode.data == pNode.next.data)
            needDelete = true;

        // 如果当前节点不需要删除,则pPreNode和pNode同时后移
        if (!needDelete)
        {
            pPreNode = pNode;
            pNode = pNode.next;
        } else
        {

            // 此时当前节点和其next都需要删除,value记录此data,
            int value = pNode.data;

            // 待删除节点toBeDel,用于判断当前节点和其next后的节点,是否还有相同的
            // Node toBeDel = pNode;
            Node toBeDel = pNext.next;

            // 如果后面还有相同的节点,那就往后寻找
            while (toBeDel != null && toBeDel.data == value)
            {
                pNext = toBeDel;
                toBeDel = pNext.next;

            }
            // 当后续没有相等的节点时
            if (pPreNode == null)// 当删除的节点是头节点时,那现在的头节点就是toBeDel
            {
                pHead = toBeDel;
            } else// 当删除的不是头节点,那pPreNode.next直接指向toBeDel,就把重复元素删除了
            {
                pPreNode.next = toBeDel;
            }
            // 最后当前节点即为toBeDel
            pNode = toBeDel;

        }

    }
    return pHead;

}

public static void main(String[] args)
{
    SingleLinkedList list = new SingleLinkedList();

    /*
     * 测试用例: a. 空 b. 一个结点 c. 两个重复结点 d. 1->1->2->3 e. 1->2->3->3->4->4
     */
    // list.addNode(1);
    // list.addNode(2);
    // list.addNode(3);
    // list.addNode(3);
    // list.addNode(3);
    // list.addNode(4);
    // list.addNode(4);
    // list.addNode(5);

    list.addNode(2);
    // list.addNode(1);
    list.addNode(1);
    list.addNode(1);
    // list.addNode(3);
    // list.addNode(4);

    System.out.println("删除重复元素前: ");
    list.printList();

    //deleteDuplication(list.head);
    Node newNode = deleteDuplication(list.head);
    list.head = newNode;
    System.out.println("删除重复元素后: ");
    // System.out.println(newNode.data);
    list.printList();
}

}

/**
*
* 单向链表的节点定义
*
*/
public class Node
{
Node next = null;
int data;

public Node(int data)
{
    this.data = data;
}

}

/**
*

  • 实现单链表的增删操作
    *
    */
    public class SingleLinkedList
    {

    Node head = null; // 链表头的引用

    /**

    • 向链表中插入数据

      • @param d:
    • 向链表中插入数据的内容,这里是末端插入
      */
      public void addNode(int d)
      {

      Node newNode = new Node(d);
      if (head == null)
      {
      head = newNode;
      return;
      }

      Node tmp = head;
      // 找到当前链表的末端
      while (tmp.next != null)
      {
      tmp = tmp.next;
      }

      // add node to end
      tmp.next = newNode;

    }

    /**

    • 向链表中插入数据

      • @param d:
    • 向链表中插入数据的内容,这里是指定位置插入 将新结点插入到index位置
      */
      public void addNode(int index, int d)
      {
      if (index > this.length() || index < 1)
      {
      System.out.println(“插入位置不合法,不存在位置” + index);
      return;
      }
      Node newNode = new Node(d);

      if (index == 1)
      {
      if (head == null)
      {
      head = newNode;
      } else
      {
      newNode.next = head;
      head = newNode;
      }
      return;
      }

      int i = 2;
      Node preNode = head;
      Node curNode = preNode.next;
      while (i != index)
      {
      preNode = curNode;
      curNode = preNode.next;
      i++;
      }

      newNode.next = curNode;
      preNode.next = newNode;

    }

    /**


      • *
    • @param index:
    • 删除第index个结点
    • */
      public boolean deleteNode(int index)
      {
      // 非法删除元素
      if (index < 1 || index > this.length())
      {
      return false;
      }

      // 删除第一个元素
      if (index == 1)
      {
      head = head.next;
      return true;
      }

      int i = 2;
      Node preNode = head;
      Node curNode = preNode.next;
      while (curNode != null)
      {
      if (i == index)
      {
      preNode.next = curNode.next;
      return true;
      }
      preNode = curNode;
      curNode = curNode.next;
      i++;
      }

      return true;
      }

    /**

      • @return 返回链表长度,即结点个数
        */
        public int length()
        {
        int length = 0;
        Node tmp = head;

      while (tmp != null)
      {
      length++;
      tmp = tmp.next;
      }

      return length;
      }

    public void printList()
    {
    Node tmp = head;
    while (tmp != null)
    {
    System.out.println(tmp.data);
    tmp = tmp.next;
    }
    }

    public void printList2(Node head)
    {
    Node tmp = head;
    while (tmp != null)
    {
    System.out.println(tmp.data);
    tmp = tmp.next;
    }
    }

    public Node orderList()
    {
    Node nextNode = null;
    int temp = 0;

    Node curNode = head;
    while (curNode.next != null)
    {
        nextNode = curNode.next;
        while (nextNode != null)
        {
            if (curNode.data > nextNode.data)
            {
                temp = curNode.data;
                curNode.data = nextNode.data;
                nextNode.data = temp;
            }
            nextNode = nextNode.next;
        }
    
        curNode = curNode.next;
    
    }
    
    return head;
    

    }

猜你喜欢

转载自blog.csdn.net/u011296723/article/details/81670532