Remove Linked List Elements

来源:互联网 发布:知乎雅思阅读技巧 编辑:程序博客网 时间:2024/06/15 00:26

这个基本功看来过了,感觉接下来就要开始啃骨头了,红红火火恍恍惚惚哼哼哈嘿啦啦啦

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } */public 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;    }}


0 0
原创粉丝点击