删除链表中等于给定值val的所有节点

2024-01-29 07:18

本文主要是介绍删除链表中等于给定值val的所有节点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

(1)删除链表中等于给定值val的所有节点 
给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。 
(2)删除链表中重复的结点 
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 
例如,链表1->2->3->3->4->4->5 处理后为 1->2->5 
(3)从尾到头打印链表

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//笔试题:</span>
<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">/*
删除链表中等于给定值val的所有节点。
样例:
给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。
*/</span>
struct Node{int value;Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> next;
};Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> CreatNode(int value, Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> next <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NULL</span>){Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> node <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new</span> Node;node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>value <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> value;node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> next;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> node;
}
Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> Delete(Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> head, int x){<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (head <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">==</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NULL</span>){<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> head;}Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> prev <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> head;Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> cur <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> head<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (cur <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">!=</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NULL</span>){<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (x <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">==</span> cur<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>value){prev<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> cur<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next;cur <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> cur<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next;}<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{prev <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> cur;cur <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> cur<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next;}}<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (x <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">==</span> head<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>value){head <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> head<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next;}<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> head;
}<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//打印链表</span>
<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">void</span> PrintList(Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> head){Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> curr <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> head;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (curr <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">!=</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NULL</span>){cout <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<</span> curr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>value <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<</span> ends;curr <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> curr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next;}cout <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<</span> endl;
}<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//面试题5:从尾到头打印链表</span>
<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">void</span> PrintListFromTail(Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> head){<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (head <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">==</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NULL</span>){<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span>;}<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{PrintListFromTail(head<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next);cout <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<</span> head<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>value <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;"><<</span> ends;}
}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li><li style="box-sizing: border-box; padding: 0px 5px;">21</li><li style="box-sizing: border-box; padding: 0px 5px;">22</li><li style="box-sizing: border-box; padding: 0px 5px;">23</li><li style="box-sizing: border-box; padding: 0px 5px;">24</li><li style="box-sizing: border-box; padding: 0px 5px;">25</li><li style="box-sizing: border-box; padding: 0px 5px;">26</li><li style="box-sizing: border-box; padding: 0px 5px;">27</li><li style="box-sizing: border-box; padding: 0px 5px;">28</li><li style="box-sizing: border-box; padding: 0px 5px;">29</li><li style="box-sizing: border-box; padding: 0px 5px;">30</li><li style="box-sizing: border-box; padding: 0px 5px;">31</li><li style="box-sizing: border-box; padding: 0px 5px;">32</li><li style="box-sizing: border-box; padding: 0px 5px;">33</li><li style="box-sizing: border-box; padding: 0px 5px;">34</li><li style="box-sizing: border-box; padding: 0px 5px;">35</li><li style="box-sizing: border-box; padding: 0px 5px;">36</li><li style="box-sizing: border-box; padding: 0px 5px;">37</li><li style="box-sizing: border-box; padding: 0px 5px;">38</li><li style="box-sizing: border-box; padding: 0px 5px;">39</li><li style="box-sizing: border-box; padding: 0px 5px;">40</li><li style="box-sizing: border-box; padding: 0px 5px;">41</li><li style="box-sizing: border-box; padding: 0px 5px;">42</li><li style="box-sizing: border-box; padding: 0px 5px;">43</li><li style="box-sizing: border-box; padding: 0px 5px;">44</li><li style="box-sizing: border-box; padding: 0px 5px;">45</li><li style="box-sizing: border-box; padding: 0px 5px;">46</li><li style="box-sizing: border-box; padding: 0px 5px;">47</li><li style="box-sizing: border-box; padding: 0px 5px;">48</li><li style="box-sizing: border-box; padding: 0px 5px;">49</li><li style="box-sizing: border-box; padding: 0px 5px;">50</li><li style="box-sizing: border-box; padding: 0px 5px;">51</li><li style="box-sizing: border-box; padding: 0px 5px;">52</li><li style="box-sizing: border-box; padding: 0px 5px;">53</li><li style="box-sizing: border-box; padding: 0px 5px;">54</li><li style="box-sizing: border-box; padding: 0px 5px;">55</li><li style="box-sizing: border-box; padding: 0px 5px;">56</li><li style="box-sizing: border-box; padding: 0px 5px;">57</li><li style="box-sizing: border-box; padding: 0px 5px;">58</li><li style="box-sizing: border-box; padding: 0px 5px;">59</li><li style="box-sizing: border-box; padding: 0px 5px;">60</li><li style="box-sizing: border-box; padding: 0px 5px;">61</li><li style="box-sizing: border-box; padding: 0px 5px;">62</li><li style="box-sizing: border-box; padding: 0px 5px;">63</li><li style="box-sizing: border-box; padding: 0px 5px;">64</li></ul>

测试

<code class="hljs autohotkey has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">void test(){Node* g = CreatNode(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">5</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>Node* f = CreatNode(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>,g)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>Node* e = CreatNode(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">4</span>,f)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>Node* d = CreatNode(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,e)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>Node* c = CreatNode(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>,d)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>Node* b = CreatNode(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">2</span>,c)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>Node* <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">a</span> = CreatNode(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>,b)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>PrintList(<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">a</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>Node* head=Delete(<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">a</span>, <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">3</span>)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>PrintList(head)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>cout << endl<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>PrintListFromTail(head)<span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">;</span>}</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li></ul>
<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//删除已排序链表中重复的结点,重复节点不保留。</span>
Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> DeleteDuplication(Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> head){<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (head <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">==</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NULL</span>){<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> head;}Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> first <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">new</span> Node;  <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//新建一个头结点就简单很多了</span>first<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> head;Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> last <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> first;Node<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">*</span> curr <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> head;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (curr <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">!=</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NULL</span> <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">&</span> curr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">!=</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NULL</span>){<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (curr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>value <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">==</span> curr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>value){int v <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> curr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>value;<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">while</span> (v <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">==</span> curr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>value){curr <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> curr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next;}last<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> curr;}<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span>{last <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> curr;curr <span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">=</span> curr<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next;}}<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> first<span class="hljs-subst" style="color: rgb(0, 0, 0); box-sizing: border-box;">-></span>next;
}</code>

这篇关于删除链表中等于给定值val的所有节点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/656145

相关文章

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

macOS无效Launchpad图标轻松删除的4 种实用方法

《macOS无效Launchpad图标轻松删除的4种实用方法》mac中不在appstore上下载的应用经常在删除后它的图标还残留在launchpad中,并且长按图标也不会出现删除符号,下面解决这个问... 在 MACOS 上,Launchpad(也就是「启动台」)是一个便捷的 App 启动工具。但有时候,应

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

Python实现将MySQL中所有表的数据都导出为CSV文件并压缩

《Python实现将MySQL中所有表的数据都导出为CSV文件并压缩》这篇文章主要为大家详细介绍了如何使用Python将MySQL数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到... python将mysql数据库中所有表的数据都导出为CSV文件到一个目录,并压缩为zip文件到另一个

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

C++从序列容器中删除元素的四种方法

《C++从序列容器中删除元素的四种方法》删除元素的方法在序列容器和关联容器之间是非常不同的,在序列容器中,vector和string是最常用的,但这里也会介绍deque和list以供全面了解,尽管在一... 目录一、简介二、移除给定位置的元素三、移除与某个值相等的元素3.1、序列容器vector、deque

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

SQL Server清除日志文件ERRORLOG和删除tempdb.mdf

《SQLServer清除日志文件ERRORLOG和删除tempdb.mdf》数据库再使用一段时间后,日志文件会增大,特别是在磁盘容量不足的情况下,更是需要缩减,以下为缩减方法:如果可以停止SQLSe... 目录缩减 ERRORLOG 文件(停止服务后)停止 SQL Server 服务:找到错误日志文件:删除

mysql删除无用用户的方法实现

《mysql删除无用用户的方法实现》本文主要介绍了mysql删除无用用户的方法实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 1、删除不用的账户(1) 查看当前已存在账户mysql> select user,host,pa

MySQL InnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据

《MySQLInnoDB引擎ibdata文件损坏/删除后使用frm和ibd文件恢复数据》mysql的ibdata文件被误删、被恶意修改,没有从库和备份数据的情况下的数据恢复,不能保证数据库所有表数据... 参考:mysql Innodb表空间卸载、迁移、装载的使用方法注意!此方法只适用于innodb_fi