Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

92.反转链表 II #162

Open
zzcyes opened this issue Sep 15, 2020 · 1 comment
Open

92.反转链表 II #162

zzcyes opened this issue Sep 15, 2020 · 1 comment
Assignees

Comments

@zzcyes
Copy link
Owner

zzcyes commented Sep 15, 2020

Title Describe
题目 92.反转链表 II
难度 ⭐⭐

题目

反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。

说明:
1 ≤ m ≤ n ≤ 链表长度。

示例:

输入: 1->2->3->4->5->NULL, m = 2, n = 4
输出: 1->4->3->2->5->NULL
zzcyes pushed a commit that referenced this issue Sep 15, 2020
zzcyes pushed a commit that referenced this issue Sep 15, 2020
zzcyes pushed a commit that referenced this issue Sep 16, 2020
@zzcyes
Copy link
Owner Author

zzcyes commented Sep 16, 2020

题解

方法一

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} m
 * @param {number} n
 * @return {ListNode}
 */
var reverseBetween = function(head, m, n) {
  let count = n - m;
  let p = (dummyHead = new ListNode());
  let pre, cur, front, tail;
  p.next = head;
  for (let i = 0; i < m - 1; i++) {
    p = p.next;
  }
  front = p;
  pre = tail = p.next;
  cur = pre.next;
  for (let i = 0; i < count; i++) {
    let next = cur.next;
    cur.next = pre;
    pre = cur;
    cur = next;
  }
  front.next = pre;
  tail.next = cur;
  return dummyHead.next;
};

zzcyes pushed a commit that referenced this issue Sep 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants