public static void QsortLinkedList(Node start, Node end) { if(start == null || end == null || start == end) return; Node pre = start; Node current=pre.Next; Node middle = start; while (current!=null && current!=end.Next) { if (current.Value < start.Value) { pre = middle; middle = middle.Next; var temp = middle.Value; middle.Value = current.Value; current.Value = temp; } current = current.Next; } var temp2 = start.Value; start.Value = middle.Value; middle.Value = temp2; QsortLinkedList(start, pre); QsortLinkedList(middle.Next, end); }
public static Node BuildLinkedList2(int[] input) { Node head = null; if (input == null || input.Length < 1) { return head; } Node preHead; foreach (var v in input) { preHead = head; head = new Node() { Value = v, Next = preHead }; } return head; }
public static Node BuildLinkedList(int[] input) { Node head = null; if (input == null || input.Length < 1) { return head; } head = new Node(); var tail = head; var pre = head; foreach (var v in input) { pre = tail; tail.Value = v; tail.Next = new Node(); tail = tail.Next; } pre.Next = null; return head; }
public static void WriteLinkedList(Node head) { while (head != null) { Console.Write(head.Value); if (head.Next != null) { Console.Write("->"); } head = head.Next; } Console.WriteLine(); }
public static void RemoveDuplicate(Node head) { if (head == null) return; HashSet<int> values = new HashSet<int>(); Node pre = head; while (head != null) { if (values.Contains(head.Value)) { pre.Next = head.Next; head = head.Next; } else { values.Add(head.Value); pre = head; head = head.Next; } } }
public static void QuickSort(Node head, Node tail) { if (head == tail) return; var oldHead = head; var l = head; while (l != tail) { while (l.Value <= head.Value && l != tail) { l = l.Next; } var r = l; while (r.Value > head.Value && r != null) { r = r.Next; } } }
public static void InsertToHead(ref Node head) { }