예제 #1
0
 public static ListNode swapPairs(ListNode head) {
   if (head.next == null || head == null) {
     return head;
   }
   /* 针对前两个结点 */
   ListNode pre = head.next, later, veryFirst;
   head.next = pre.next;
   pre.next = head;
   //		later = head.next;
   head = pre;
   later = head.next;
   /*
    * 针对后续结点
    * 连续有2个结点,才进行换位
    */
   while (later.next != null && later.next.next != null) {
     veryFirst = later;
     pre = pre.next.next;
     later = later.next.next;
     pre.next = later.next;
     later.next = pre;
     veryFirst.next = later;
     later = pre;
     pre = veryFirst.next;
   }
   return head;
 }
예제 #2
0
  public void reorderList(ListNode head) {
    if (head == null || head.next == null || head.next.next == null) {
      return;
    }
    ListNode oneStep = head;
    ListNode twoStep = head;

    while (twoStep != null && twoStep.next != null) {
      twoStep = twoStep.next.next;
      oneStep = oneStep.next;
    }

    ListNode half = oneStep.next;
    oneStep.next = null;

    Stack<ListNode> stack = new Stack<ListNode>();
    while (half != null) {
      stack.push(half);
      half = half.next;
    }

    ListNode tmp = head;

    while (head != null) {
      ListNode oldRight = head.next;
      if (stack.empty()) {
        break;
      }
      head.next = stack.pop();
      head.next.next = oldRight;
      head = oldRight;
    }
    head = tmp;
  }
예제 #3
0
  public ListNode removeNthFromEnd(ListNode head, int n) {
    // Start typing your Java solution below
    // DO NOT write main() function
    if (head == null) {
      return null;
    }

    ListNode tail = head;
    int i = 0;
    for (; i < n && tail != null; ++i) {
      tail = tail.next;
    }

    if (tail == null && i < n) {
      return null;
    }

    ListNode nth = head;
    ListNode pHead = new ListNode(0);
    pHead.next = head;
    ListNode prev = pHead;
    while (tail != null) {
      prev = nth;
      nth = nth.next;
      tail = tail.next;
    }

    // remove nth from end
    prev.next = nth.next;

    return pHead.next;
  }
예제 #4
0
  public ListNode partition(ListNode head, int x) {
    if ((head == null) || (head.next == null)) return head;

    ListNode h1 = null, t1 = null, h2 = null, t2 = null, p = head;
    while (p != null) {
      if (p.val < x) {
        if (h1 == null) {
          h1 = p;
          t1 = p;
        } else {
          t1.next = p;
          t1 = t1.next;
        }
        p = p.next;
      } else {
        if (h2 == null) {
          h2 = p;
          t2 = p;
        } else {
          t2.next = p;
          t2 = t2.next;
        }
        p = p.next;
      }
    }
    if (t1 == null) {
      t2.next = null;
      return h2;
    } else {
      t1.next = h2;
      if (t2 != null) t2.next = null;
      return h1;
    }
  }
예제 #5
0
 // fail to solve the problem
 // This is very basic and important!
 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
   // only one statement is enough to solve 3 condition
   // cause linklist is connected, I don't need to insert rest of the node anymore
   if (l1 == null || l2 == null) return l1 == null ? l2 : l1;
   // the head node is specil, need seperate initiate.
   ListNode head = null;
   if (l1.val <= l2.val) {
     head = l1;
     l1 = l1.next;
   } else {
     head = l2;
     l2 = l2.next;
   }
   ListNode temp = head;
   // in while loop, temp.next = smallvalue; l1 = l1.next; temp = temp.next;
   while (l1 != null && l2 != null) {
     if (l1.val <= l2.val) {
       temp.next = l1;
       l1 = l1.next;
     } else {
       temp.next = l2;
       l2 = l2.next;
     }
     ;
     temp = temp.next;
   }
   // finally, we find the non-null node add it to the rear of l3
   // cause linklist is connected, I don't need to insert rest of the node anymore
   temp.next = l1 == null ? l2 : l1;
   return head;
 }
예제 #6
0
 public static ListNode reverseKGroup(ListNode head, int k) {
   if (head == null || head.next == null || k < 2) {
     return head;
   }
   LinkedList<ListNode> l = new LinkedList<ListNode>();
   int count = 0;
   ListNode pre = new ListNode(0);
   ListNode temp = pre;
   while (head != null) {
     if (count++ < k) {
       ListNode t = new ListNode(head.val);
       l.push(t);
       head = head.next;
     }
     if (count >= k) {
       count = 0;
       while (!l.isEmpty()) {
         temp.next = l.pop();
         temp = temp.next;
       }
     }
   }
   while (!l.isEmpty()) {
     temp.next = l.pollLast();
     temp = temp.next;
   }
   return pre.next;
 }
예제 #7
0
  public ListNode swapPairs(ListNode head) {
    if (head == null) {
      return null;
    }
    if (head.next == null) {
      return head;
    }
    ListNode root = new ListNode(-1);
    ListNode last = root;
    ListNode current1 = head;
    ListNode current2 = head.next;
    ListNode next = head.next;

    while (current2 != null) {
      next = current2.next;
      last.next = current2;
      current2.next = current1;
      current1.next = next;

      last = current1;
      current1 = next;
      if (next == null) {
        break;
      }
      current2 = next.next;
    }

    return root.next;
  }
예제 #8
0
  public ListNode reverseBetween(ListNode head, int m, int n) {
    if (m == n) return head;
    ListNode newhead = new ListNode(0);
    newhead.next = head;
    m++;
    n++;
    ListNode node = newhead;
    ListNode nodeB4Rev = null;
    ListNode prev = null;
    ListNode next = null;

    for (int i = 1; i < m; i++) {
      nodeB4Rev = node;
      node = node.next;
    }
    next = node.next;
    prev = null;
    for (int i = m; i < n; i++) {
      ListNode temp = next.next;
      next.next = node;
      prev = node;
      node = next;
      next = temp;
    }
    nodeB4Rev.next.next = next;
    nodeB4Rev.next = node;
    return newhead.next;
  }
예제 #9
0
    public ListNode reverseBetween(ListNode head, int m, int n) {
      if (m >= n || head == null) return null;

      ListNode origin = new ListNode(-1);
      origin.next = head;

      int i = 1;
      ListNode pre = origin;
      while (pre != null && i < m) {
        i++;
        pre = pre.next;
      }

      ListNode mNode = pre.next;
      ListNode cNode = mNode.next;

      while (cNode != null && i < n) {
        i++;
        ListNode temp = cNode.next;
        cNode.next = pre.next; // pre.next always points to the first
        // node in the reverse list
        mNode.next = temp;
        pre.next = cNode;

        cNode = temp;
      }

      return origin.next;
    }
예제 #10
0
  private ListNode reverseListDirection(ListNode head) {
    if (head == null) {
      return null;
    }
    if (head.next == null) {
      return head;
    }

    ListNode cur = head.next;
    ListNode preReverse;

    if (head.next.next != null) {
      preReverse = head.next.next;
      head.next = null;

      while (preReverse != null) {
        cur.next = head;
        head = cur;
        cur = preReverse;
        preReverse = preReverse.next;
      }

      cur.next = head;
      head = cur;

    } else {
      cur.next = head;
      head.next = null;
      head = cur;
    }

    return head;
  }
예제 #11
0
  public ListNode Segregate(ListNode head) {
    if (head == null) {
      return head;
    }

    ListNode last = head;
    while (last.next != null) {
      last = last.next;
    }

    ListNode now = last;
    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode previous = dummy;
    while (previous.next != last) {
      head = previous.next;
      if (head.val % 2 != 0) {
        previous.next = previous.next.next;
        now.next = head;
        now = now.next;
      } else {
        previous = previous.next;
      }
    }
    now.next = null;

    return dummy.next;
  }
  public ListNode reverseBetween(ListNode head, int m, int n) {
    ListNode dummy = new ListNode(0);
    ListNode d = dummy;
    ListNode p = head;

    int i = 1;
    while (i < m) {
      d.next = p;
      d = d.next;
      p = p.next;
      i++;
    }
    d.next = null;

    while (i <= n) {
      ListNode q = p;
      p = p.next;
      q.next = d.next;
      d.next = q;
      i++;
    }

    while (d.next != null) d = d.next;
    d.next = p;
    return dummy.next;
  }
예제 #13
0
  public static void reorderList(ListNode head) {
    if (head == null || head.next == null || head.next.next == null) {
      return;
    }
    ListNode preHead = new ListNode(-1);
    preHead.next = head;
    ListNode fast = preHead;
    ListNode slow = preHead;
    while (fast != null && fast.next != null) {
      slow = slow.next;
      fast = fast.next.next;
    }
    fast = slow.next;
    slow.next = null;
    slow = head;
    fast = reverse(fast);

    ListNode p0 = preHead;
    ListNode p1 = slow;
    ListNode p2 = fast;
    while (p2 != null) {
      p0.next = p1;
      ListNode tmp1 = p1.next;
      p1.next = p2;
      p0 = p2;
      p1 = tmp1;
      p2 = p2.next;
    }
    p0.next = p1;
  }
 public ListNode deleteDuplicates(ListNode head) {
   if (head == null) {
     return head;
   }
   ListNode dummy = new ListNode(0);
   dummy.next = head;
   ListNode prev = dummy;
   boolean dup = false;
   while (head != null) {
     if (head.next != null && head.val == head.next.val) {
       head = head.next;
       dup = true;
     } else if (head.next == null) {
       if (dup) {
         prev.next = null;
       }
       head = head.next;
     } else if (head.val != head.next.val) {
       if (dup) {
         head = head.next;
         prev.next = head;
         dup = false;
       } else {
         prev = head;
         head = head.next;
       }
     }
   }
   return dummy.next;
 }
  public ListNode oddEvenList2(ListNode head) {
    if (head == null || head.next == null) {
      return head;
    }
    ListNode dummyFirst = new ListNode(0);
    ListNode dummySecond = new ListNode(0);
    ListNode temp = head;
    ListNode tempFirst = dummyFirst;
    ListNode tempSecond = dummySecond;
    int counter = 0;
    while (temp != null) {
      if (counter % 2 == 0) {
        dummyFirst.next = temp;
        dummyFirst = temp;
      } else {
        dummySecond.next = temp;
        dummySecond = temp;
      }

      counter++;
      temp = temp.next;
    }

    ListNode runner = tempFirst;
    while (runner.next != null) {
      runner = runner.next;
    }
    runner.next = tempSecond.next;

    return tempFirst.next;
  }
예제 #16
0
  /** Use two lists to sort all the nodes, one with larger nodes, the other with smaller nodes */
  public static ListNode partition(ListNode head, int k) {
    ListNode sHead = new ListNode(-1); // dummy head
    ListNode lHead = new ListNode(-1); // dummy head

    ListNode sTail = sHead;
    ListNode lTail = lHead;

    ListNode n = head;
    while (n != null) {
      if (n.val < k) {
        sTail.next = n;
        sTail = sTail.next;
      } else {
        lTail.next = n;
        lTail = lTail.next;
      }

      n = n.next;
    }

    // append the large list to the small list
    sTail.next = lHead.next;
    lTail.next = null;

    // adjust the head of the small list to delete the dummy node
    return sHead.next;
  }
예제 #17
0
  public static void reorderList(ListNode head) {
    if (head != null && head.next != null) {
      ListNode slow = head;
      ListNode fast = head;

      // use a fast and slow pointer to break the link to two parts.
      while (fast != null && fast.next != null) {
        slow = slow.next;
        fast = fast.next.next;
      }

      ListNode second = slow.next;
      slow.next = null; // need to close first part
      second = reverseOrder(second); // reverse order for second part

      ListNode p1 = head;
      ListNode p2 = second;

      // merge two lists here
      while (p2 != null) {
        ListNode temp1 = p1.next;
        ListNode temp2 = p2.next;

        p1.next = p2;
        p2.next = temp1;

        p1 = temp1;
        p2 = temp2;
      }
    }
  }
  /**
   * Use pre and cur to store the node before the sublist and head of sublist Insert next to head
   * each time to reverse a list Stop when the next of current node is next group's head Go ahead
   * move pre and cur, and reverse the next group
   */
  public ListNode reverseKGroup(ListNode head, int k) {
    if (head == null || head.next == null || k < 0) return head;
    if (k == 0 || k == 1) return head;

    ListNode dummy = new ListNode(0);
    dummy.next = head;
    ListNode pre = dummy; // current group dummy
    ListNode cur = head; // current group head

    while (cur != null) {
      ListNode p = pre.next;
      int group = k;
      while (p != null && group > 0) { // move p to next head as stop node
        group--;
        p = p.next;
      }
      if (group > 0) break; // end of list
      while (cur.next != p) { // note it's cur.next here
        ListNode nt = cur.next.next; // insert cur.next to head
        cur.next.next = pre.next;
        pre.next = cur.next;
        cur.next = nt; // unlink cur with cur next, link with next next
      }
      pre = cur; // move pre to current group's tail
      cur = cur.next; // move cur to next group's head
    }

    return dummy.next;
  }
 public boolean isPalindrome(ListNode head) {
   if (head == null || head.next == null) {
     return true;
   }
   ListNode dummy = new ListNode(0);
   dummy.next = head;
   ListNode pre, start, cur;
   ListNode fast = dummy;
   ListNode mid = dummy;
   int count = 0;
   while (fast != null && fast.next != null) {
     fast = fast.next.next;
     mid = mid.next;
     count++;
   }
   if (fast == null) count--;
   pre = mid;
   start = pre.next;
   cur = start.next;
   for (int i = 1; i < count; ++i) {
     start.next = cur.next;
     cur.next = pre.next;
     pre.next = cur;
     cur = start.next;
   }
   ListNode c1 = dummy.next, c2 = mid.next;
   for (int i = 0; i < count; ++i) {
     if (c1.val != c2.val) {
       return false;
     }
     c1 = c1.next;
     c2 = c2.next;
   }
   return true;
 }
예제 #20
0
 public ListNode rotateRight(ListNode head, int k) {
   int len = 0;
   ListNode cur = head;
   while (cur != null) {
     ++len;
     cur = cur.next;
   }
   if (len == 0) {
     return null;
   }
   k = len - k % len;
   cur = head;
   ListNode sentinel = new ListNode(0);
   sentinel.next = cur;
   ListNode pre = sentinel;
   while (k > 0) {
     pre = pre.next;
     --k;
   }
   sentinel.next = pre.next;
   pre.next = null;
   pre = sentinel;
   while (pre.next != null) {
     pre = pre.next;
   }
   pre.next = head;
   return sentinel.next;
 }
  public ListNode getRev(ListNode head) {
    if (head == null) {
      return null;
    }
    ListNode lh = head;
    ListNode pre, p, l1, l2;
    ListNode head_proto, nextPair;
    if (lh.next == null) {
      return lh;
    } else {
      head_proto = lh.next;
      pre = new ListNode(-1);
      pre.next = head_proto;
      while (lh.next != null) {
        p = lh;
        l1 = p;
        l2 = l1.next;
        nextPair = l2.next;

        pre.next = l2;
        lh = l2;
        lh.next = l1;
        l1.next = nextPair;
        pre = l1;
        lh = nextPair;
        if (lh == null) {
          break;
        }
      }
      return head_proto;
    }
  }
 /*
  *       m              n
  *  1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
  *  ^    ^    ^    ^
  * pre  cur   p1   p2
  *
  *  1 -> 2 <- 3    4 -> 5 -> 6 -> 7
  *  ^         ^    ^    ^
  * pre       cur   p1   p2
  *
  *  1 -> 2 <- 3 <- 4    5 -> 6 -> 7
  *  ^              ^    ^    ^
  * pre            cur  p1   p2
  *
  *  1 -> 2 <- 3 <- 4 <- 5 -> 6 -> 7
  *  ^                   ^    ^    ^
  * pre                 cur  p1   p2
  *
  *       m              n
  *  1 -> 2 <- 3 <- 4 <- 5 -> 6 -> 7
  *  ^
  * pre
  *
  * */
 public ListNode reverseBetween(ListNode head, int m, int n) {
   if (head.next == null || m == n) return head;
   ListNode dummy = new ListNode(-1);
   dummy.next = head;
   ListNode pre = dummy, cur = head, p1 = null, p2 = null;
   // First, find pre & cur
   for (int i = 1; i < m; i++) {
     pre = cur;
     cur = cur.next;
   }
   // p1 is cur's next
   if (cur.next != null) {
     p1 = cur.next;
   }
   // p2 is p1's next
   if (p1 != null) {
     p2 = p1.next;
   }
   // point p1's next to cur
   // move forward cur, p1, p2
   for (int i = m; i < n; i++) {
     p1.next = cur;
     cur = p1;
     p1 = p2;
     if (p2 != null) {
       p2 = p2.next;
     }
   }
   // link reversed segment
   pre.next.next = p1;
   pre.next = cur;
   return dummy.next;
 }
예제 #23
0
 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
   ListNode dummy = new ListNode(0);
   ListNode cur = dummy;
   while (l1 != null || l2 != null) {
     if (l1 != null && l2 != null) {
       if (l1.val <= l2.val) {
         cur.next = l1;
         l1 = l1.next;
       } else {
         cur.next = l2;
         l2 = l2.next;
       }
       cur = cur.next;
     } else if (l1 == null && l2 != null) {
       cur.next = l2;
       l2 = l2.next;
       cur = cur.next;
     } else if (l1 != null && l2 == null) {
       cur.next = l1;
       l1 = l1.next;
       cur = cur.next;
     }
   }
   return dummy.next;
 }
예제 #24
0
 public ListNode partition(ListNode head, int x) {
   ListNode less = null;
   ListNode equal_more = null;
   ListNode p = null;
   ListNode q = null;
   while (head != null) {
     if (head.val < x) {
       if (less == null) {
         less = head;
         p = less;
       } else {
         p.next = head;
         p = p.next;
       }
     } else {
       if (equal_more == null) {
         equal_more = head;
         q = equal_more;
       } else {
         q.next = head;
         q = q.next;
       }
     }
     head = head.next;
   }
   if (less == null) return equal_more;
   head = less;
   p.next = equal_more;
   if (q != null) q.next = null;
   return head;
 }
  public ListNode deleteDuplicates(ListNode head) {
    if (head == null) {
      return null;
    }

    Set<Integer> valToRemove = new HashSet<Integer>();
    ListNode cur = head;
    while (cur != null) {
      if (cur.next != null && cur.val == cur.next.val) {
        valToRemove.add(cur.val);
      }
      cur = cur.next;
    }
    ListNode fake = new ListNode(0);
    fake.next = head;
    cur = fake;
    while (cur.next != null) {
      if (valToRemove.contains(cur.next.val)) {
        cur.next = cur.next.next;
        continue;
      }
      cur = cur.next;
    }
    return fake.next;
  }
예제 #26
0
 public ListNode reverseBetween(ListNode head, int m, int n) {
   // Start typing your Java solution below
   // DO NOT write main() function
   ListNode p, q, r, s;
   int k = n - m;
   if (m == 1) {
     q = head;
     s = q.next;
     for (int i = 0; i < k; i++) {
       r = s.next;
       s.next = q;
       q = s;
       s = r;
     }
     head.next = s;
     head = q;
     return head;
   }
   p = head;
   for (int i = 2; i < m; i++) {
     p = p.next;
   }
   q = p;
   s = q.next;
   r = s.next;
   for (int i = 0; i < k; i++) {
     q = s;
     s = r;
     r = r.next;
     s.next = q;
   }
   p.next.next = r;
   p.next = s;
   return head;
 }
예제 #27
0
    public ListNode swapPairs(ListNode head) {
      if (head == null || head.next == null) {
        return head;
      }
      ListNode top = head.next;
      ListNode first = new ListNode(0);
      while (head != null) {
        if (head.next == null) {
          first.next = head;
          first = head;
          break;
        }
        // first代表上一组的最后一个元素,用来和本组的第一个元素链起来
        ListNode next = head.next;
        first.next = next;
        first = head;

        // 本组的两个元素交互,并把head指向下一组的第一个元素
        ListNode nextHead = next.next;
        next.next = head;
        head = nextHead;
      }
      first.next = null;
      return top;
    }
예제 #28
0
  private ListNode removeNode(ListNode head, int pos) {
    if (pos == 0) {
      return head.next;
    }
    if (pos == 1) {
      return head.next;
    } else {
      int count = 1;
      ListNode previous = head;
      ListNode next = head.next;

      while (next != null && count < pos - 1) {
        previous = next;
        next = next.next;
        count++;
      }

      if (next == null) {
        previous.next = null;
      } else {
        previous.next = next.next;
      }

      return head;
    }
  }
 public ListNode plusAB(ListNode a, ListNode b) {
   if (a == null) return b;
   if (b == null) return a;
   ListNode curA = a;
   ListNode curB = b;
   ListNode preA = null;
   int carry = 0;
   while (curA != null && curB != null) {
     int sum = curA.val + curB.val + carry;
     if (sum >= 10) {
       carry = 1;
       sum = sum - 10;
     } else {
       carry = 0;
     }
     curA.val = sum;
     preA = curA;
     curA = curA.next;
     curB = curB.next;
   }
   if (curB != null) {
     curB.val += carry;
     carry = 0;
     preA.next = curB;
   }
   if (curA != null) {
     curA.val += carry;
     carry = 0;
   }
   if (carry != 0) {
     preA.next = new ListNode(carry);
   }
   return a;
 }
  /**
   * @param ListNode head is the head of the linked list
   * @return: ListNode head of the linked list
   */
  public static ListNode deleteDuplicates(ListNode head) {
    // write your code here
    if (head == null || head.next == null) {
      return head;
    }

    ListNode curt = new ListNode(0);
    ListNode ret = curt;

    while (head != null && head.next != null) {
      if (head.val != head.next.val) {
        curt.next = new ListNode(head.val);
        curt = curt.next;
        head = head.next;
      } else {
        while (head.next != null && head.val == head.next.val) {
          head = head.next;
        }
        head = head.next;
      }
    }
    if (head != null) {
      curt.next = new ListNode(head.val);
    }
    return ret.next;
  }