给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
示例 1:
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
示例 2:
输入:head = [], val = 1
输出:[]
示例 3:
输入:head = [7,7,7,7], val = 7
输出:[]
提示:
列表中的节点数目在范围 [0, 104] 内
1 <= Node.val <= 50
0 <= val <= 50
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/remove-linked-list-elements
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
package cn.fansunion.leecode.linkedlist;
/**
* 203. 移除链表元素
* 给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。
力扣
* @author [email protected]
*
* 2022-2-18
*/
public class RemoveLinkedListElements {
/* 输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
示例 2:
输入:head = [], val = 1
输出:[]
示例 3:
输入:head = [7,7,7,7], val = 7
输出:[]*/
public ListNode removeElements(ListNode head, int val) {
//特殊情况判断
//head.next==null也可以走标准流程,也需要判断头结点是否需要删除
if (head == null /*|| head.next==null*/) {
return head;
}
//头结点先不判定,从第2个节点判断是否为val
ListNode cur = head;
while(cur!=null && cur.next!=null) {
if(cur.next.val==val) {
//删除目标节点:跳过目标节点
cur.next=cur.next.next;
}else {
//下一个节点
cur=cur.next;
}
}
//头结点,是否为val
if(head.val==val) {
return head.next;
}
//头结点不是val的情况
return head;
}
}
package test.leecode.linkedlist;标签:head,ListNode,3615,val,next,链表,移除,new,newHead From: https://blog.51cto.com/fansunion/6056771
import org.junit.Assert;
import org.junit.Test;
import cn.fansunion.leecode.linkedlist.ListNode;
import cn.fansunion.leecode.linkedlist.RemoveLinkedListElements;
/**
* @author [email protected]
*
* 2022-2-23
*/
public class RemoveLinkedListElementsTest {
/* 输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
示例 2:
输入:head = [], val = 1
输出:[]
示例 3:
输入:head = [7,7,7,7], val = 7
输出:[]*/
@Test
public void test1263456() {
RemoveLinkedListElements list = new RemoveLinkedListElements();
//1,1,1,2,3,3
ListNode node62 = new ListNode(6,null);
ListNode node5 = new ListNode(5,node62);
ListNode node4 = new ListNode(4,node5);
ListNode node3 = new ListNode(3,node4);
ListNode node61 = new ListNode(6,node3);
ListNode node2 = new ListNode(2,node61);
ListNode head1 = new ListNode(1,node2);
ListNode newHead=list.removeElements(head1, 6);
Assert.assertEquals(1, newHead.val);
Assert.assertEquals(2, newHead.next.val);
Assert.assertEquals(3, newHead.next.next.val);
Assert.assertEquals(4, newHead.next.next.next.val);
Assert.assertEquals(5, newHead.next.next.next.next.val);
}
@Test
public void test7777() {
RemoveLinkedListElements list = new RemoveLinkedListElements();
//7,7,7,7
ListNode node73 = new ListNode(7,null);
ListNode node72 = new ListNode(7,node73);
ListNode node71 = new ListNode(7,node72);
ListNode head7 = new ListNode(7,node71);
ListNode newHead=list.removeElements(head7, 7);
Assert.assertNull(newHead);
}
@Test
public void test2777() {
RemoveLinkedListElements list = new RemoveLinkedListElements();
//7,7,7,7
ListNode node73 = new ListNode(7,null);
ListNode node72 = new ListNode(7,node73);
ListNode node71 = new ListNode(7,node72);
ListNode head2 = new ListNode(2,node71);
ListNode newHead=list.removeElements(head2, 7);
Assert.assertEquals(2, newHead.val);
Assert.assertNull(newHead.next);
}
}