当前位置:   article > 正文

LeetCode_203. 移除链表元素_leetcode移除链表元素

leetcode移除链表元素
  1. public class S_203 {
  2. public ListNode removeElements(ListNode head, int val) {
  3. // 为空
  4. if (head == null)
  5. return null;
  6. // 当不为空 且链表值等于给定值时
  7. while (head != null && head.val == val) {
  8. // 位移一位
  9. head = head.next;
  10. }
  11. ListNode node = head;
  12. while (node.next != null) {
  13. if (node.next.val == val)
  14. node.next = node.next.next;
  15. else
  16. node = node.next;
  17. }
  18. return head;
  19. }
  20. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/836254
推荐阅读
相关标签
  

闽ICP备14008679号