Home > Backend Development > PHP Tutorial > How to delete duplicate nodes in a linked list in php? (code example)

How to delete duplicate nodes in a linked list in php? (code example)

不言
Release: 2023-04-04 07:50:02
Original
1501 people have browsed it

The content of this article is about how to delete duplicate nodes in the linked list in PHP? (Code example) has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Delete duplicate nodes in the linked list:

  1. Define two pointers pre and current

  2. The two pointers move backward at the same time. If the current pointer has the same value as the next node, it will move forward alone until there is no equal

  3. pre pointer next points directly to the back of the current pointer One, skip all the same ones

pre=linkList
current=linkList
while current!=null
    if current->data==current->next->data
        value=current->data
        while value==current->next->data
            current=current->next
        pre->next=current->next
    pre=pre->next
    current=current->next
return linkList
Copy after login
<?php
class Node{
        public $data;
        public $next;
        public function __construct($data=""){
                $this->data=$data;
        }   
}
//构造一个带重复的链表
$linkList=new Node();
$linkList->next=null;
$temp=$linkList;
$node1=new Node(2);
$temp->next=$node1;
$temp=$node1;
$node2=new Node(2);
$temp->next=$node2;
$temp=$node2;
$node3=new Node(3);
$temp->next=$node3;
$temp=$node3;
$node4=new Node(3);
$temp->next=$node4;
$temp=$node4;
$node5=new Node(4);
$temp->next=$node5;
$node5->next=null;
function deleteDuplication($pHead){
        $pre=$pHead->next;//当前都指向第一个结点
        $current=$pHead->next;//当前结点是第一个结点
        while($current!=null){
                //如果当前结点值和当前结点的下一个结点值相同
                if($current->next!=null && $current->data==$current->next->data){
                        //保存当前结点值
                        $val=$current->data;
                        //当前结点往后移直到和下一个结点值不相等
                        while($current->next!=null && $val==$current->next->data){
                                $current=$current->next;
                        }   
                        //前一个指针next直接指向当前结点的next
                        $pre->next=$current->next;
                }   
                //两个指针同时后移
                $pre=$pre->next;
                $current=$current->next;
        }
        return $pHead;
}
var_dump($linkList);
$result=deleteDuplication($linkList);
var_dump($result);
Copy after login
object(Node)#1 (2) {
  ["data"]=>
  string(0) ""
  ["next"]=>
  object(Node)#2 (2) {
    ["data"]=>
    int(2)
    ["next"]=>
    object(Node)#3 (2) {
      ["data"]=>
      int(2)
      ["next"]=>
      object(Node)#4 (2) {
        ["data"]=>
        int(3)
        ["next"]=>
        object(Node)#5 (2) {
          ["data"]=>
          int(3)
          ["next"]=>
          object(Node)#6 (2) {
            ["data"]=>
            int(4)
            ["next"]=>
            NULL
          }
        }
      }
    }
  }
}
object(Node)#1 (2) {
  ["data"]=>
  string(0) ""
  ["next"]=>
  object(Node)#2 (2) {
    ["data"]=>
    int(2)
    ["next"]=>
    object(Node)#4 (2) {
      ["data"]=>
      int(3)
      ["next"]=>
      object(Node)#6 (2) {
        ["data"]=>
        int(4)
        ["next"]=>
        NULL
      }
    }
  }
}
Copy after login

Related recommendations:

How to realize the k-th node from the bottom of the output linked list in php (code example)

How to print a linked list from end to head in php (code example)

The above is the detailed content of How to delete duplicate nodes in a linked list in php? (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template