public static int GetValueIntAtv1(Node head, int idx) { if (head == null) { throw new ArgumentNullException("Head not found."); } if (idx < 0) { throw new ArgumentOutOfRangeException("Invalid index."); } int listLength = LinkedListHelper.GetLinkedListLength(head); if (idx > listLength) { throw new ArgumentOutOfRangeException("Index is out of range."); } Node ref1 = head; Node ref2 = head; for (int i = 0; i < idx; i++) { ref1 = ref1.Next; } while (ref1 != null) { ref1 = ref1.Next; ref2 = ref2.Next; } return(ref2.Value); }
public static int GetValueIntAtv2(Node head, int idx) { if (head == null) { throw new ArgumentNullException("Head not found."); } if (idx < 0) { throw new ArgumentOutOfRangeException("Invalid index."); } int listLength = LinkedListHelper.GetLinkedListLength(head); if (idx > listLength) { throw new ArgumentOutOfRangeException("Index is out of range."); } Node iterator = head; int count = listLength - idx; for (int i = 0; i < count; i++) { iterator = iterator.Next; } return(iterator.Value); }
public static Node IsCircle(Node head) { Node iterator = head; if (iterator == null) { return(null); } int idx = 0; int length = LinkedListHelper.GetLinkedListLength(head); int[] buffer = new int[length]; while (iterator != null) { if (!buffer.Contains(iterator.Value)) { buffer[idx] = iterator.Value; idx++; } else { return(iterator); } iterator = iterator.Next; } return(null); }
public static bool IsPalindrome_v2(Node head) { Node iterator = head; if (iterator == null) { throw new ArgumentNullException("Head not found."); } int length = LinkedListHelper.GetLinkedListLength(head); int idx = 0, endIdx = 0, startIdx = 0; int[] buffer; bool isEven = length % 2 == 0; bool isPalindome = true; if (isEven) { buffer = new int[length / 2 - 1]; endIdx = length / 2 - 1; startIdx = length / 2; } else { buffer = new int[(length - 1) / 2 - 1]; endIdx = (length - 1) / 2 - 1; startIdx = (length - 1) / 2 + 1; } while (iterator != null) { if (idx <= endIdx) { buffer[idx] = iterator.Value; idx++; } else if (idx >= startIdx) { idx--; if (buffer[idx] != iterator.Value) { return(false); } } else { idx++; } iterator = iterator.Next; } return(isPalindome); }
public static Node GetIntersection(Node list1, Node list2) { if (list1 == null || list2 == null) { return(null); } int length1 = LinkedListHelper.GetLinkedListLength(list1); int length2 = LinkedListHelper.GetLinkedListLength(list2); Node iterator1 = length1 <= length2 ? list1 : list2; Node iterator2 = length1 <= length2 ? list2 : list1; Node result = null; Node iterator3 = result; while (iterator1 != null) { iterator2 = list2; while (iterator2 != null) { if (iterator1.Value == iterator2.Value) { if (iterator3 == null) { iterator3 = new Node(iterator1.Value); result = iterator3; } else { iterator3.Next = new Node(iterator1.Value); iterator3 = iterator3.Next; } iterator2 = iterator2.Next; list1 = iterator1; list2 = iterator2; break; } else { if (result != null) { return(result); } } iterator2 = iterator2.Next; } iterator1 = iterator1.Next; } return(result); }
public static Node Addition2_v2(Node h1, Node h2) { int val1 = 0, val2 = 0; int idx = LinkedListHelper.GetLinkedListLength(h1) - 1; while (h1 != null) { val1 += h1.Value * (int)Math.Pow(10, idx); idx--; } idx = LinkedListHelper.GetLinkedListLength(h2) - 1; while (h2 != null) { val2 += h2.Value * (int)Math.Pow(10, idx); idx--; } string result = (val1 + val2).ToString(); Node headRes = null; Node iterator = headRes; for (int i = 0; i < result.Length; i++) { if (headRes == null) { headRes = new Node(result[i]); } else { iterator.Next = new Node(result[i]); iterator = iterator.Next; } } return(headRes); }
public static bool IsPalindrome_v1(Node head) { if (head == null || head.Next == null) { return(false); } Node iterator = head; int linkedListLength = LinkedListHelper.GetLinkedListLength(head); //Stack<int> intStack = new Stack<int>(); int[] intStack = new int[linkedListLength / 2]; int count = 0, i = -1; bool paired = linkedListLength % 2 == 0; if (paired) { while (iterator != null) { if (count >= linkedListLength / 2) { if (intStack[i] != iterator.Value) { return(false); } i--; } else { intStack[count] = iterator.Value; count++; i++; } iterator = iterator.Next; } return(true); } else { bool first = true; while (iterator != null) { if (count >= linkedListLength / 2) { if (first) { first = false; } else { if (intStack[i] != iterator.Value) { return(false); } i--; } } else { intStack[count] = iterator.Value; count++; i++; } iterator = iterator.Next; } return(true); } }