常規實現
常規實現的思路比較簡單
遍歷連結串列,找到需要刪除的節點的前一個節點,使用prev標記位置prev->next = prev->next->next遞迴實現拿連結串列6 -> 7 -> 8 -> null來舉例
8節點不等於7,於是返回了8節點7節點等於7,於是返回了7節點的下一個節點86節點連線8節點,變成6 -> 8 -> nullpublic class SinglyLinkedListDeleted { Node head; // head of list class Node { int data; Node next; Node(int d) { data = d; next = null; } } // 常規實現 void deleteNode(int key) { // Store head node. Node temp = head, prev = null; // If head node itself holds the key to be deleted. if (temp != null && temp.data == key) { head = temp.next; return; } // Search for the key to be deleted, keep track of // the previous node as we need to change. while (temp != null && temp.data != key) { prev = temp; temp = temp.next; } // If key was not present in linked list. if (temp == null) return; // Unlink the node from linked list. prev.next = temp.next; } // 連結串列遞迴刪除輸入存在的元素 public Node deleteNode(Node head, int val) { if (head == null) return null; head.next = deleteNode(head.next, val); return head.data == val ? head.next : head; } public void push(int data) { Node node = new Node(data); node.next = head; head = node; } public static void main(String[] args) { SinglyLinkedListDeleted list = new SinglyLinkedListDeleted(); list.push(8); list.push(9); list.push(7); list.push(6); Node current = list.head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); list.deleteNode(9); list.head = list.deleteNode(list.head, 7); current = list.head; while (current != null) { System.out.print(current.data + " "); current = current.next; } }}
輸出
6 7 9 8 6 8