Featured image of post 203. Remove Linked List Elements

203. Remove Linked List Elements

203. Remove Linked List Elements

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
/*
 * @lc app=leetcode.cn id=203 lang=java
 *
 * [203] 移除链表元素
 *
 * https://leetcode.cn/problems/remove-linked-list-elements/description/
 *
 * algorithms
 * Easy (56.20%)
 * Likes:    1404
 * Dislikes: 0
 * Total Accepted:    706.2K
 * Total Submissions: 1.3M
 * Testcase Example:  '[1,2,6,3,4,5,6]\n6'
 *
 * 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
 * 
 * 
 * 示例 1:
 * 
 * 
 * 输入:head = [1,2,6,3,4,5,6], val = 6
 * 输出:[1,2,3,4,5]
 * 
 * 
 * 示例 2:
 * 
 * 
 * 输入:head = [], val = 1
 * 输出:[]
 * 
 * 
 * 示例 3:
 * 
 * 
 * 输入:head = [7,7,7,7], val = 7
 * 输出:[]
 * 
 * 
 * 
 * 
 * 提示:
 * 
 * 
 * 列表中的节点数目在范围 [0, 10^4] 内
 * 1 
 * 0 
 * 
 * 
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */

class Solution {
    public ListNode removeElements(ListNode head, int val) {
        // 头节点 是满足 Node.val == val 的节点,删除头节点,
        // 但是next节点的值可能也相等,所以循环遍历
        while(head != null && head.val == val){
            head = head.next;
        }
        if (head == null) {
            return head;
        }

        ListNode cur = head; 
        while (cur.next != null) {
            if(cur.next.val == val){
                cur.next = cur.next.next;
            } else {
                cur = cur.next;
            }
        }
        return head;

    }
}


// class Solution {
// public ListNode removeElements(ListNode head, int val) {
//     ListNode dummy = new ListNode(0);
//     dummy.next = head;
//     ListNode prev = dummy;
//     ListNode cur = head;
//     while (cur != null) {
//         if (cur.val == val) {
//             prev.next = cur.next;
//         } else {
//             prev = cur;
//         }
//         cur = cur.next;
//     }
//     return dummy.next;
// }
// }
// @lc code=end
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy