7.2:链表删除给定值

7.2:链表删除给定值

 

 1 public static Node removeValue(Node head, int num) {
 2         // head来到第一个不需要删的位置
 3         while (head != null) {
 4             if (head.value != num) {
 5                 break;
 6             }
 7             head = head.next;
 8         }
 9         // 1 ) head == null
10         // 2 ) head != null
11         Node pre = head;
12         Node cur = head;
13         while (cur != null) {
14             if (cur.value == num) {
15                 pre.next = cur.next;
16             } else {
17                 pre = cur;
18             }
19             cur = cur.next;
20         }
21         return head;
22     }

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐