您的位置:首页 > 其它

LeetCode - Remove Linked List Elements

2015-04-26 22:16 369 查看
删除链表中的指定元素。

/**
* 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;
ListNode p,q;
p = head.next;
q = head;

while(p!=null) {
if(p.val == val) {
q.next = p.next;
p = p.next;
}
else {
p = p.next;
q = q.next;
}
}
if(head.val == val) {
head = head.next;
}

return head;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: