给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。返回删除后的链表的头节点。
1.此题对比原题有改动
2.题目保证链表中节点的值互不相同
3.该题只会输出返回的链表和结果做对比,所以若使用 C 或 C++ 语言,你不需要 free 或 delete 被删除的节点
数据范围:
0<=链表节点值<=10000
0<=链表长度<=10000
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。返回删除后的链表的头节点。
{2,5,1,9},5{2,1,9}
给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 2 -> 1 -> 9
{2,5,1,9},1{2,5,9}
给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 2 -> 5 -> 9
public ListNode deleteNode (ListNode head, int val) {
// write code here
ListNode temp = new ListNode(0);
temp = head;
// find the node for deleting
if (head.val != val){
while(head.next.val != val){
head = head.next;
}
// delete the node
head.next = head.next.next;
} else{
// delete the node
temp = head.next;
}
return temp;
}
}
public ListNode deleteNode (ListNode head, int val) {
ListNode preNode = null;
ListNode curNode = head;
while (curNode != null) {
ListNode next = curNode.next;
if (curNode.val == val) {
curNode.next = null;
if (preNode == null) {
head = next;
} else
preNode.next = next;
if (next == null)
break;
} else
preNode = curNode;
curNode = next;
}
return head;
} public class Solution {
public ListNode deleteNode (ListNode head, int val) {
ListNode p = head;
ListNode q = p.next;
if (head.val == val) return head.next;
while (q.val != val) {
p = q;
q = q.next;
}
p.next = q.next;
return head;
}
} public ListNode deleteNode (ListNode head, int val) {
// write code here
if(head.val != val){
head.next = deleteNode(head.next, val);
}
if(head.val == val){
head = head.next;
}
return head;
}
public class Solution {
public ListNode deleteNode (ListNode head, int val) {
// write code here
ListNode temp = head;
if(head.val == val){
return head.next;
}
while(head.next!= null){
if(head.next.val == val){
head.next = head.next.next;
}else{
head = head.next;
}
}
return temp;
}
} import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类
* @param val int整型
* @return ListNode类
*/
public ListNode deleteNode (ListNode head, int val) {
if(head==null) return null;
if(head.val==val) return head.next;
ListNode point = head;
ListNode next = head.next;
while(true){
if(next!=null){
if(next.val==val){
point.next = next.next;
return head;
}else {
point = next;
next = next.next;
}
}else {
return null;
}
}
}
} import java.util.*;
public class Solution {
public ListNode deleteNode (ListNode head, int val) {
if(head.val==val){//如果要删除第一个结点
ListNode second=head.next;
head.next=null;
return second;
}
ListNode pointNode=head;
boolean found=false;
while(true){
if(pointNode.next==null){
break;
}
if(pointNode.next.val==val){
found=true;
break;
}
pointNode=pointNode.next;
}
if(found){
pointNode.next=pointNode.next.next;
}
return head;
}
} public ListNode deleteNode (ListNode head, int val) {
// write code here
if(head == null) return null;
ListNode cur = head;
ListNode pre = null;
if(head.val == val) return head.next;
while(cur != null) {
if(cur.val == val) {
pre.next = cur.next;
}
pre = cur;
cur = cur.next;
}
return head;
} import java.util.*;
public class Solution {
public ListNode deleteNode (ListNode head, int val) {
//加入一个头节点
ListNode res = new ListNode(0);
res.next = head;
//前序节点
ListNode pre = res;
//当前节点
ListNode cur = head;
//遍历链表
while(cur != null){
if(cur.val == val){
pre.next = cur.next;
break;
}
pre = cur;
cur = cur.next;
}
//返回去掉头节点
return res.next;
}
}
public ListNode deleteNode (ListNode head, int val) {
// write code here
ListNode dummpy = new ListNode(-1);
dummpy.next = head;
ListNode p = dummpy;
ListNode cur = head;
while (cur.val != val) {
p = cur;
cur = cur.next;
}
p.next = p.next.next;
return dummpy.next;
}