1. 程式人生 > >LC_237.Delete Node in a Linked List

LC_237.Delete Node in a Linked List

body des 當前 cep turn 沒有 calling bsp oid

https://leetcode.com/problems/delete-node-in-a-linked-list/description/

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.

 1 //時間 O(1)-因為沒有循環遍歷 空間 (1)
 2 public class LC_237_DeleteNodeInALinkedList {
 3     public void deleteNode(ListNode node) {
 4         if (node == null) return ;
 5         //這道題不好,因為沒有給頭,所以無法遍歷 也沒有辦法用IF 來判斷
 6         //假設當前節點就是NODE NODE 去搶下一個節點的值,然後再跳過去。 這裏不用考慮地址空間這些
 7         node.val = node.next.val ;
8 node.next = node.next.next ; 9 } 10 }

LC_237.Delete Node in a Linked List