Example #1
0
  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;
  }
Example #2
0
  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;
  }
Example #3
0
 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;
 }