public LinkedList.LinkedNode reverseBetween(LinkedList.LinkedNode head, int m, int n) { LinkedList.LinkedNode pre_hard = new LinkedNode(-1); pre_hard.Next = head; LinkedList.LinkedNode pre = pre_hard, cur = head, next = head.Next, lefts = null,lefte = null, rights = null,righte; int count = m - 1; while (count -- > 0) { next = cur.Next; pre = cur; cur = next; } count = n - m+1; while (count-- > 0) { next = cur.Next; if (lefte == null) { lefte = cur; lefts = pre; } else { cur.Next = pre; } pre = cur; cur = next; } rights = pre; righte = cur; lefts.Next = rights; lefte.Next = righte; return pre_hard.Next; }
public LinkedNode deleteDuplicates(LinkedNode head) { LinkedNode prehead = new LinkedNode(-1); prehead.Next = head; LinkedNode pre = prehead, cur = head, next = head.Next; while (cur != null) { next = cur.Next; LinkedNode looper = next; bool skip = false; while (looper != null && looper.Data.Equals(cur.Data)) { skip = true; next = looper; looper = looper.Next; } if (skip) { pre.Next = next.Next; cur = next.Next; } else { pre = cur; cur = next; } } return prehead.Next; }
private BinaryTreeNode DFS(int s, int e) { if (looper == null || s > e) { return null; } int m = s + (e - s) / 2; BinaryTreeNode left = DFS( s, m - 1); BinaryTreeNode root = new BinaryTreeNode(looper.Data); looper = looper.Next; root.LeftNode = left; root.RightNode = DFS( m + 1, e); return root; }
public BinaryTreeNode sortedListToBST(LinkedNode head) { looper = head; return DFS(0,head.getLength()); }
public LinkedNode mergeTwoLists(LinkedNode l1, LinkedNode l2) { LinkedNode head = null, output = null; while (l1 != null && l2 != null) { if (l1 == null) { if (head == null) { head = l2; output = l2; } else { head.Next = l2; head = l2; } l2 = l2.Next; } else if (l2 == null) { if (head == null) { head = l1; output = l1; } else { head.Next = l1; head = l1; } l1 = l1.Next; } else { if (l1.Data <= l2.Data) { if (head == null) { head = l1; output = l1; } else { head.Next = l1; head = l1; } l1 = l1.Next; } else { if (head == null) { head = l2; output = l2; } else { head.Next = l2; head = l2; } l2 = l2.Next; } } } return output; }