美文网首页
LintCode入门级-4

LintCode入门级-4

作者: ___shin | 来源:发表于2017-11-01 23:05 被阅读0次

    描述:
    删除链表中等于给定值val的所有节点。
    样例:
    给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。
    实现:

    /**
     * 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 listnode = new ListNode(0);
            listnode.next = head;
            head = listnode;
            while(head.next != null) {
                if(head.next.val == val) {
                    head.next = head.next.next;
                } else {
                    head = head.next;
                }
            }
            return listnode.next;
        }
    }
    

    相关文章

      网友评论

          本文标题:LintCode入门级-4

          本文链接:https://www.haomeiwen.com/subject/doynpxtx.html