删除链表中给定数值
作者:
简朴_ | 来源:发表于
2020-11-23 12:41 被阅读0次public class Code03_DeleteGivenValue {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
public static Node removeValue(Node head, int num) {
/**
*
* 1) 先来到第一个不用删除的位置,默认head开始有很多重复的数字。
* 2)
*
* */
while (head != null) {
if (head.value != num) {
break;
}
head = head.next;
}
// head、pre、cur来到 第一个不需要删的位置
Node pre = head;
Node cur = head;
// 在剩下的节点中把num节点全删除,把head返回
while (cur != null) {
if (cur.value == num) {
pre.next = cur.next;
} else {
pre = cur;
}
cur = cur.next;
}
return head;
}
}
本文标题:删除链表中给定数值
本文链接:https://www.haomeiwen.com/subject/hpapiktx.html
网友评论