public class LinkedList<T> { // The head node of the linked list private Node<T> head; // The length of the linked list private int length;
public LinkedList() { head = null; length = 0; }
public int Length => length; public bool IsEmpty() => (length == 0);
// Add a new node at the beginning of the linked list public void AddFirst(T data) { Node<T> newNode = new Node<T>(data); newNode.Next = head; // Update the new node as the head of the linked list head = newNode; length++; } // Add a new node at the end of the linked list public void AddTail(T data) { Node<T> newNode = new Node<T>(data); if (head == null) { head = newNode; return; } Node<T> current = head; while (current.Next != null) { current = current.Next; } current.Next = newNode; length++; }
public void DeleteElemets(T Data) { Node<T> dummyHead = new Node<T>(default(T)); dummyHead.Next = head; Node<T> temp = dummyHead; while (temp.Next != null) { if (temp.Next.Data.Equals(Data)) { temp.Next = temp.Next.Next; length--; } else { temp = temp.Next; } } // return dummyHead.Next; } public void Delete(T Data) { if (head == null) return; if (head.Data.Equals(Data)) { head = head.Next; length--; return; } Node<T> current = head; while (current != null && current.Next != null) { if (current.Next.Data.Equals(Data)) { current.Next = current.Next.Next; length--; return; } current = current.Next; } } public void DeleteAtHead() { if (head == null) return; head = head.Next; length--; } public void Find(T Data) { if (head == null) { Console.WriteLine("The Linked List is null, can't find the data"); return; } Node<T> current = head; while (current != null) { if (current.Data.Equals(Data)) { Console.WriteLine("Found the data: " + Data); return; } current = current.Next; } Console.WriteLine("Not Found the data: " + Data); }
public void InsertAt(int index, T data) { if (index < 0) { throw new ArgumentOutOfRangeException("Index", index, "Index must be non - negative"); } Node<T> newNode = new Node<T>(data); if (index == 0) { AddFirst(data); return; } Node<T> current = head; for (int i = 0; i < index - 1; i++) { if (current == null || current.Next == null) { throw new ArgumentOutOfRangeException("Index", index, "Index is out of range"); } current = current.Next; } newNode.Next = current.Next; current.Next = newNode; length++; }
public void Print() { Node<T> current = head; if (current == null) { Console.WriteLine("null"); return; } while (current != null) { Console.Write(current.Data + "->"); current = current.Next; } Console.Write("null"); Console.WriteLine(); } }
补充练习:
如何反转链表?
时间复杂度为 O(n),空间复杂度为 O(1)。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public void ReverseList() { if (head.Next == null || head.Next.Next == null) { return; } Node<T> pre = null; Node<T> current = head; Node<T> temp = null; while (current != null) { // Store the key node to prevent data loss temp = current.Next; current.Next = pre; pre = current; current = temp; } head = pre; }
public Node<T> FindKthToLast(int k) { if (head == null || k <= 0) { return null; } Node<T> fast = head; Node<T> slow = head; // Let the fast pointer move k steps first, and the remaining is N - K steps, which is the K - th step from the end for (int i = 0; i < k; i++) { if (fast == null) { return null; } fast = fast.Next; } // Move the fast and slow pointers together, and the slow pointer moves N - K steps while (fast != null) { fast = fast.Next; slow = slow.Next; } return slow; }