public ListNode swapPairs(ListNode head) { if (head == null) return null; if (head.next == null) return head; ListNode newHead = head.next; head.next = swapPairs(newHead.next); newHead.next = head; return newHead; }
public ListNode createList(int[] intArray, int linkIndex) { ListNode head = new ListNode(intArray[0]); ListNode previousNode = head; ListNode linkedBackNode = null; for (int i = 1; i < intArray.length; i++) { ListNode node = new ListNode(intArray[i]); if (linkIndex == i) linkedBackNode = node; previousNode.next = node; previousNode = node; } previousNode.next = linkedBackNode; return head; }
public ListNode deleteDuplicates(ListNode head) { if (head == null) return null; if (head.next == null) return head; head.next = deleteDuplicates(head.next); if (head.val == head.next.val) return head.next; else return head; }