static void Main(string[] args) { ListNode l1 = new ListNode(1); ListNode two = new ListNode(3); ListNode three = new ListNode(6); ListNode four = new ListNode(7); ListNode l2 = new ListNode(2); ListNode six = new ListNode(4); ListNode seven = new ListNode(6); ListNode eight = new ListNode(8); l1.next = two; two.next = three; three.next = four; l2.next = six; six.next = seven; seven.next = eight; TreeNode t1 = new TreeNode(1); TreeNode t2 = new TreeNode(2); TreeNode t3 = new TreeNode(3); TreeNode t4 = new TreeNode(1); TreeNode t5 = new TreeNode(3); //TreeNode t6 = new TreeNode(6); //7TreeNode t7 = new TreeNode(7); //TreeNode t8 = new TreeNode(8); t1.left = t2; t1.right = t3; t2.left = t4; t2.right = t5; //t4.left = t7; //t3.right = t6; //t6.right = t8; IList < IList < int >> test = Submissions.Generate(4); Console.Write(Submissions.HasPathSum(t1, 6)); Console.In.ReadLine(); }
public static ListNode OddEvenList(ListNode head) { if (head == null) return null; ListNode currentOddNode = head; ListNode currentEvenNode = head.next; ListNode evenNodeHead = head.next; while (currentOddNode.next != null) { if (currentOddNode.next.next != null) { currentOddNode.next = currentOddNode.next.next; currentOddNode = currentOddNode.next; } else { currentOddNode.next = null; } if (currentEvenNode.next != null) { if (currentEvenNode.next.next != null) { currentEvenNode.next = currentEvenNode.next.next; currentEvenNode = currentEvenNode.next; } else { currentEvenNode.next = null; } } } currentOddNode.next = evenNodeHead; return head; }
public static ListNode MergeTwoLists(ListNode l1, ListNode l2) { ListNode current = l1; ListNode head = current; ListNode temp = null; while (l1 != null || l2 != null) { if (l1 != null && l2 != null) { if (l1.val <= l2.val) { current.next = l1; l1 = l1.next; } else { current = l2; l2 = l2.next; } } else if (l1 != null) { current = l1; l1 = l1.next; } else { current = l2; l2 = l2.next; } current = current.next; } return head; }
public static ListNode GetIntersectionNode(ListNode headA, ListNode headB) { ListNode curA = headA; ListNode curB = headB; int lengthA = 0, lengthB = 0; if(headA == null || headB == null) { return null; } while(curA != null) { lengthA++; curA = curA.next; } while(curB != null) { lengthB++; curB = curB.next; } curA = headA; curB = headB; if(lengthA > lengthB) { for(int i = 1; i <= lengthA - lengthB; i++) { curA = curA.next; } } else if(lengthB > lengthA) { for (int i = 1; i <= lengthB - lengthA; i++) { curB = curB.next; } } while(curA != null || curB != null) { if(curA == curB) { return curA; } else { curA = curA.next; curB = curB.next; } } return null; }
public static void DeleteNode(ListNode node) { if (node.next == null) return; else { node.val = node.next.val; node.next = node.next.next; } }
public static ListNode DeleteDuplicates(ListNode head) { if (head == null) { return null; } ListNode currentNode = head; while (currentNode.next != null) { if (currentNode.val == currentNode.next.val) { currentNode.next = currentNode.next.next; } else { currentNode = currentNode.next; } } return head; }