public ListNode RemoveNthFromEnd(ListNode head, int n) { if (head == null) return null; if (head.next == null && n >= 1) return null; ListNode start = new ListNode(0); ListNode slow = start, fast = start; start.next = head; for (int i = 0; i < n; i++) { fast = fast.next; } //Move fast to the end, maintaining the gap while (fast.next != null) { slow = slow.next; fast = fast.next; } //Skip the desired node slow.next = slow.next.next; return start.next; }
public ListNode SwapPairs1(ListNode head) { if (head == null) return null; if (head.next == null) return head; ListNode oddList = new ListNode(0); ListNode oddHead = oddList; ListNode evenList = new ListNode(0); ListNode evenHead = evenList; while (head != null) { oddList.next = head; oddList = oddList.next; evenList.next = head.next; if (head.next != null) { head = head.next.next; evenList = evenList.next; } if (head == null) oddList.next = null; } ListNode result = new ListNode(0); ListNode pointer = result; evenHead = evenHead.next; oddHead = oddHead.next; while (oddHead != null && evenHead != null) { pointer.next = evenHead; evenHead = evenHead.next; pointer = pointer.next; pointer.next = oddHead; oddHead = oddHead.next; pointer = pointer.next; } while (oddHead != null) { pointer.next = oddHead; oddHead = oddHead.next; pointer = pointer.next; } return result.next; }
public ListNode RemoveNthFromEnd1(ListNode head, int n) { if (head == null) return null; if (head.next == null && n >= 1) return null; int index = 0; head = GetRemovedNext(head, n, ref index); if (index == n) { return head.next; } return head; }
private ListNode GetRemovedNext(ListNode listNode, int n, ref int index) { if (listNode.next == null) { index = 1; return listNode; } listNode.next = GetRemovedNext(listNode.next, n, ref index); if (index++ == n) { listNode.next = listNode.next.next; } return listNode; }
public ListNode SwapPairs(ListNode head) { if (head == null) return null; if (head.next == null) return head; ListNode result = new ListNode(0); ListNode pointer = result; while (head != null && head.next != null) { ListNode node = head.next.next; pointer.next = SwapTwoNodes(head, head.next); head = node; pointer = pointer.next.next; } return result.next; }
public void DoIt() { ListNode node1 = new ListNode(1); ListNode node2 = new ListNode(2); ListNode node3 = new ListNode(3); ListNode node4 = new ListNode(4); node1.next = node2; node2.next = node3; //node3.next = node4; ListNode result = SwapPairs(node1); while (result != null) { Console.WriteLine(result.val); result = result.next; } }
public void DoIt() { ListNode node1 = new ListNode(1); ListNode node2 = new ListNode(2); //ListNode node3 = new ListNode(3); //ListNode node4 = new ListNode(4); //ListNode node5 = new ListNode(5); node1.next = node2; //node2.next = node3; //node3.next = node4; //node4.next = node5; ListNode result = RemoveNthFromEnd(node1, 1); while (result != null) { Console.WriteLine(result.val); result = result.next; } }
public ListNode MergeTwoLists(ListNode l1, ListNode l2) { if (l1 == null) return l2; if (l2 == null) return l1; ListNode result = new ListNode(0); ListNode root = result; while (l1 != null && l2 != null) { if (l1 != null && l2 != null && l1.val <= l2.val) { result.next = l1; l1 = l1.next; result = result.next; } else if (l1 != null && l2 != null && l1.val > l2.val) { result.next = l2; l2 = l2.next; result = result.next; } } while (l1 != null) { result.next = l1; l1 = l1.next; result = result.next; } while (l2 != null) { result.next = l2; l2 = l2.next; result = result.next; } return root.next; }
public void DoIt() { ListNode node1 = new ListNode(1); ListNode node2 = new ListNode(2); ListNode node3 = new ListNode(3); ListNode node4 = new ListNode(4); ListNode node5 = new ListNode(5); ListNode node6 = new ListNode(6); ListNode node7 = new ListNode(7); node1.next = node3; node2.next = node4; node3.next = node5; node4.next = node6; node6.next = node7; ListNode result = MergeTwoLists(node1, node2); while (result != null) { Console.WriteLine(result.val); result = result.next; } }
private ListNode SwapTwoNodes(ListNode node1, ListNode node2) { if (node2 == null) return null; ListNode afterNode = node2.next; node2.next = node1; node1.next = afterNode; return node2; }