203. Remove Linked List Elements

来源:互联网 发布:弯矩图绘制软件 编辑:程序博客网 时间:2024/05/16 05:02

题目:https://leetcode.com/problems/remove-linked-list-elements/

代码:

/** * 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) {        if(head == null)            return null;        while(head!=null&&head.val == val)        {            head = head.next;        }        ListNode temp = head;        while(temp!=null&&temp.next!=null)        {            if(temp.next.val==val)            {                temp.next = temp.next.next;            }            else                temp = temp.next;        }        return head;    }}2ms========================================================
0 0
原创粉丝点击