p237

LeetCode 237 Delete Node in a Linked List 题解

1.题目:

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

题意:

删除链表中的一个节点

2.解题思路:

链表

3.代码


[title] [] [url] [link text]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 
package leetcode;

import java.util.Scanner;

import tool.ListNode;

public class p237 {
public static void main(String[] args) {

ListNode aListNode = new ListNode(1);
ListNode a2 = new ListNode(2);
ListNode a3 = new ListNode(3);
ListNode a4 = new ListNode(4);
aListNode.next = a2;
a2.next = a3;
a3.next = a4;
a4.next = null;
deleteNode(a3);

}

public static void deleteNode(ListNode node) {
if (node.next != null) {
node.val = node.next.val;
node.next = node.next.next;
}
}

}




4.一些总结:

链表啦