public ListNode DeleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; ListNode dummy = head; while (dummy.next != null) { if (dummy.next.val == dummy.val) { dummy.next = dummy.next.next; } else dummy = dummy.next; } return head; }
public ListNode DeleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; ListNode fakedHead = new ListNode(-1); fakedHead.next = head; ListNode prevHead = fakedHead; ListNode prevDiff = fakedHead.next; while (prevDiff != null) { if (prevDiff.next != null && prevDiff.val == prevDiff.next.val) { int currV = prevDiff.val; while (prevDiff.next != null) { prevDiff = prevDiff.next; if (prevDiff.next == null || prevDiff.next.val != currV) { break; } } } if (prevHead.next == prevDiff) { prevHead = prevHead.next; } else { prevHead.next = prevDiff.next; } prevDiff = prevDiff.next; // else // { // prevHead.next = prevDiff; // prevHead = prevHead.next; // // prevDiff = prevDiff.next; // } } return fakedHead.next; }
//相同的即是交点 public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { ListNode p1 = headA; ListNode p2 = headB; if (p1 == null || p2 == null) return null; while (p1 != null && p2 != null && p1 != p2) { p1 = p1.next; p2 = p2.next; if (p1 == p2) return p1; if (p1 == null) p1 = headB; if (p2 == null) p2 = headA; } return p1; }
public ListNode RemoveNthFromEnd(ListNode head, int n) { ListNode head1 = head; ListNode head2 = head; while (n-- > 0) { head2 = head2.next; } if (head2 == null) { return head.next; } while (head2.next != null) { head1 = head1.next; head2 = head2.next; } head1.next = head1.next.next; return head; // ListNode start = new ListNode(0); // ListNode slow = start, fast = start; // slow.next = head; // // //Move fast in front so that the gap between slow and fast becomes n // for (int i = 1; i <= n + 1; i++) // { // fast = fast.next; // } // //Move fast to the end, maintaining the gap // while (fast != null) // { // slow = slow.next; // fast = fast.next; // } // //Skip the desired node // slow.next = slow.next.next; // return start.next; }
public void Execute() { ListNode node1 = new ListNode(1); ListNode node2 = new ListNode(1); ListNode node3 = new ListNode(1); // ListNode node4 = new ListNode(2); // ListNode node5 = new ListNode(3); // ListNode node6 = new ListNode(4); // ListNode node7 = new ListNode(4); node1.next = node2; node2.next = node3; node3.next = null; // node2.next = node3; // node3.next = node4; // node4.next = node5; // node5.next = node6; // node6.next = node7; // node7.next = null; DeleteDuplicates(node1); }
public ListNode MergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return(l2); } if (l2 == null) { return(l1); } if (l1.val < l2.val) { l1.next = MergeTwoLists(l1.next, l2); return(l1); } else { l2.next = MergeTwoLists(l1, l2.next); return(l2); } }
public ListNode DeleteDuplicates(ListNode head) { if (head == null || head.next == null) return head; ListNode fakedHead = new ListNode(-1); fakedHead.next = head; ListNode prevHead = fakedHead; ListNode prevDiff = fakedHead.next; while (prevDiff != null) { if (prevDiff.next != null && prevDiff.val == prevDiff.next.val) { int currV = prevDiff.val; while (prevDiff.next != null) { prevDiff = prevDiff.next; if (prevDiff.next == null || prevDiff.next.val != currV) { break; } } } if (prevHead.next == prevDiff) { prevHead = prevHead.next; } else { prevHead.next = prevDiff.next; } prevDiff = prevDiff.next; } return fakedHead.next; }
public ListNode MergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) { return l2; } if (l2 == null) { return l1; } if (l1.val < l2.val) { l1.next = MergeTwoLists(l1.next, l2); return l1; } else { l2.next = MergeTwoLists(l1, l2.next); return l2; } }
public void DeleteNode(ListNode node) { ListNode next = node.next; node.val = next.val; node.next = next.next; }