package CountDownListNode;public class CountDown {public class ListNode{int val;ListNode next;ListNode (int x){val = x;}}public ListNode getKthFromEnd(ListNode head , int k){ListNode front = head , behind = head;while(front != null && k>0){front = front.next;k--;}while(front !=null){front = front.next;behind = behind.next;}return behind;}public ListNode buildLinkedList(int[] nums){ListNode dummy = new ListNode(0);ListNode curr = dummy;for(int num : nums){ListNode newNode = new ListNode(num);curr.next = newNode;curr = curr.next;}return dummy.next;}}

Java 链表操作:从尾部开始查找第 k 个节点和构建链表

原文地址: http://www.cveoy.top/t/topic/pYkW 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录