LintCode 第452题 删除链表中的元素

来源:互联网 发布:wkwebview js调用原生 编辑:程序博客网 时间:2024/06/05 17:15
描述:

          删除链表中等于给定值val的所有节点。

样例:
          给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。

代码实现:

 

public ListNode removeElements(ListNode head, int val) {        // Write your code here        ListNode node1 = head;        if(head==null){            return null;        }        while(head.next!=null){            if(head.next.val==val){                if(head.next.next!=null){                    head.next = head.next.next;                }else{                    head.next=null;                    break;                }            }else{                head = head.next;            }        }        if(node1.val == val){            return node1.next;        }                        return node1;    }


0 0
原创粉丝点击