输入一个链表,反转链表后,输出链表的所有元素。使得只扫描一次链表。
思路:两个指针p,q。p先扫K个,然后p,q同时向后,p到表尾的时候,q所指向的就是倒数k个节点。注意代码的鲁棒性(健壮性)排除异常输入。
package com.mytest.mymain;class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}public class FindKthtotail { public static void main(String[] args) { FindKthtotail findKthtotail=new FindKthtotail(); ListNode One =new ListNode(2); ListNode Two =new ListNode(3); ListNode Third =new ListNode(4); ListNode Four =new ListNode(5); ListNode Five =new ListNode(6); One.next=Two; Two.next= Third; Third.next=Four; Four.next=Five; ListNode head=new ListNode(1); head.next=One; ListNode result=findKthtotail.FindKthToTail(head,3); System.out.PRintln(result.val); } public ListNode FindKthToTail(ListNode head, int k) { if (head == null || k <= 0) return null; ListNode pListNode = head, qListNode = head; for (int i = 1; i < k; ++i) { if (pListNode.next != null) { pListNode = pListNode.next; } else { return null; } } while (pListNode.next != null) { qListNode = qListNode.next; pListNode = pListNode.next; } return qListNode; }}
新闻热点
疑难解答