PHP 求链表并集

  1 <?php

  2     #合并两个非降序链表并去重,并且不能修改原链表数据

  3     #非降序,则链表内部有可能重复

  4 

  5     class Node {

  6         public $data = null;

  7         public $next = null;

  8     }

  9 

 10     function traverse($head) {

 11         while ($head != null) {

 12             echo $head->data . " ";

 13             $head = $head->next;

 14         }

 15     }

 16 

 17     #此处假设两个链表均不为空

 18     function merge_linkedlist($a, $b) {

 19         $c = new Node();

 20         if ($a->data < $b->data) {

 21             $c->data = $a->data;   

 22             $anext = $a->next;

 23             $bnext = $b;

 24         } else {

 25             $c->data = $b->data;

 26             $anext = $a;

 27             $bnext = $b->next;

 28         }

 29 

 30         $cpre = $c;

 31 

 32         #其实是一个添加了去重复功能的合并排序而已

 33         while ($anext != null && $bnext != null) {

 34             if ($anext < $bnext) {

 35                 #去重复

 36                 if ($anext->data != $cpre->data) {

 37                     $cnext = new Node();

 38                     $cnext->data = $anext->data;

 39                     $cpre->next = $cnext;

 40                     $cpre = $cnext;

 41                 }

 42                 $anext = $anext->next;

 43             } else {

 44                 if ($bnext->data != $cpre->data) {

 45                     $cnext = new Node();

 46                     $cnext->data = $bnext->data;

 47                     $cpre->next = $cnext;

 48                     $cpre = $cnext;

 49                 }

 50                 $bnext = $bnext->next;

 51             }

 52         }

 53 

 54         while ($anext != null) {

 55             if ($cpre->data != $anext->data) {

 56                 $cnext = new Node();

 57                 $cnext->data = $anext->data;

 58                 $cpre->next = $cnext;

 59                 $cpre = $cnext;

 60             }

 61             $anext = $anext->next;

 62         }

 63 

 64         while ($bnext != null) {

 65             if ($cpre->data != $bnext->data) {

 66                 $cnext = new Node();

 67                 $cnext->data = $bnext->data;

 68                 $cpre->next = $cnext;

 69                 $cpre = $cnext;

 70             }

 71             $bnext = $bnext->next;

 72         }

 73 

 74         return $c;

 75     }

 76 

 77     $a = new Node();

 78     $a1 = new Node();

 79     $a2 = new Node();

 80     $a3 = new Node();

 81     $a4 = new Node();

 82     $b = new Node();

 83     $b1 = new Node();

 84     $b2 = new Node();

 85     $b3 = new Node();

 86     $b4 = new Node();

 87     $a->data = 0;

 88     $a1->data = 2;

 89     $a2->data = 2;

 90     $a3->data = 3;

 91     $a4->data = 4;

 92     $b->data = 1;

 93     $b1->data = 2;

 94     $b2->data = 2;

 95     $b3->data = 3;

 96     $b4->data = 5;

 97     $a->next = $a1;

 98     $a1->next = $a2;

 99     $a2->next = $a3;

100     $a3->next = $a4;

101     $b->next = $b1;

102     $b1->next = $b2;

103     $b2->next = $b3;

104     $b3->next = $b4;

105 

106     $c = merge_linkedlist($a, $b);

107     traverse($a);

108     echo "<br>";

109     traverse($b);

110     echo "<br>";

111     traverse($c);

112 ?>

你可能感兴趣的:(PHP)