LeetCode OJ 203Remove Linked List Elements

Posted xujian_2014

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode OJ 203Remove Linked List Elements相关的知识,希望对你有一定的参考价值。

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

题目:Remove all elements from a linked list of integers that have value val.

Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5

解题思路:题意为移除链表中指定元素的节点,示例代码:

public class Solution

    public ListNode removeElements(ListNode head, int val) 
    
     if(head==null)
			 return null;
		 while(head.val==val)
		 
			 if(head.next==null)
				 return null;
			 else
				 head=head.next;
		 
		 ListNode p=head;
		 ListNode q=head.next;
		 while(q!=null)
		 
			 if(q.val==val)
			 
				 if(q.next!=null)
				 
					 q=q.next;
					 p.next=q;
				 
				 else
				 
				      p.next=null;
					 break;
				 
			 
			 else
			 
				 if(q.next!=null)
				 
					 p=q;
					 q=q.next;
				 
				 else
				 
					 break;
				 
			 
		 
		 return head;
    


以上是关于LeetCode OJ 203Remove Linked List Elements的主要内容,如果未能解决你的问题,请参考以下文章

[LeetCode]203. Remove Linked List Elements

#Leetcode# 203. Remove Linked List Elements

Java [Leetcode 203]Remove Linked List Elements

leetcode?python 203. Remove Linked List Elements

203. Remove Linked List Elements - LeetCode

LeetCode 203. Remove Linked List Elements