Code Monkey home page Code Monkey logo

Comments (3)

PisecesPeng avatar PisecesPeng commented on September 25, 2024

解题思路

  • 快慢指针, 遍历找出节点, 跳过.

代码

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
    }
}
private static ListNode func(ListNode head, int n) {
   ListNode newHead = new ListNode(0);
   newHead.next = head;
   // 双指针法
   ListNode head1 = newHead;
   ListNode head2 = newHead;
   // head1 提前先走n步
   while ((n--) > 0) {
       head1 = head1.next;
   }
   // head1到链表最后, head1、head2之间间隔n
   while (head1.next != null) {
       head1 = head1.next;
       head2 = head2.next;
   }

   if (head1 != head2)
       head2.next = head2.next.next;

   return newHead.next;
}

from pisecespeng.record.me.

PisecesPeng avatar PisecesPeng commented on September 25, 2024

LeetCode题解

解题思路

  • 根据栈先进后出的原则,
  • 全部进栈后, 弹出的第n个节点就是需要删除的节点,
  • 得到其前节点, 再跳过待删除节点即可.

代码

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
    }
}
public ListNode func(ListNode head, int n) {
    ListNode dummy = new ListNode(0, head);
    Deque<ListNode> stack = new LinkedList<ListNode>();
    ListNode cur = dummy;
    while (cur != null) {
        stack.push(cur);
        cur = cur.next;
    }
    for (int i = 0; i < n; ++i) {
        stack.pop();
    }
    ListNode prev = stack.peek();
    prev.next = prev.next.next;
    ListNode ans = dummy.next;
    return ans;
}

from pisecespeng.record.me.

PisecesPeng avatar PisecesPeng commented on September 25, 2024

LeetCode题解

解题思路

  • 快慢指针, 遍历找出节点, 跳过.

代码

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
        val = x;
    }
}
public ListNode func(ListNode head, int n) {
    ListNode dummy = new ListNode(0, head);
    ListNode first = head;
    ListNode second = dummy;
    for (int i = 0; i < n; ++i) {
        first = first.next;
    }
    while (first != null) {
        first = first.next;
        second = second.next;
    }
    second.next = second.next.next;
    ListNode ans = dummy.next;
    return ans;
}

from pisecespeng.record.me.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.