找出单向链表中的一个节点,该节点到尾指针的距离为K。链表的倒数第0个结点为链表的尾指针。要求时间复杂度为O(n)。
链表结点定义如下:
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
}
链表节点的值初始化为1,2,3,4,5,6,7。
该节点到尾指针的距离K
返回该单向链表的倒数第K个节点,输出节点的值
2
6
请自觉实现一个链表,将1到7依次加入链表,然后再寻找倒数第K个节点。要求加节点与找节点的操作复杂度均为O(n)。
import java.util.*; class ListNode{ int val; ListNode next=null; ListNode(int val){ this.val=val; } } public class Main{ public static void main(String[]args){ Scanner sc=new Scanner(System.in); ListNode head=new ListNode(1); ListNode p=null; p=head; p.next=null; for(int i=2;i<8;i++){ ListNode q=new ListNode(i); p.next=q; p=q; p.next=null; } p=helper(head,sc.nextInt()); System.out.println(p.val); } public static ListNode helper(ListNode head,int k){ ListNode slow=head,fast=head; for(int i=0;i<k;i++){ fast=fast.next; } while(fast!=null){ fast=fast.next; slow=slow.next; } return slow; } }
import java.util.Scanner; /** * @Date: 2020-05-05 10:49 * @version: 1.0 */ class ListNode{ ListNode next; int val; public ListNode(int val) { this.val = val; } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int k = sc.nextInt(); ListNode head = new ListNode(1); ListNode pre = head; ListNode cur; for (int i = 2; i <= 7; i++){//初始化 cur = new ListNode(i); pre.next = cur; pre = cur; } ListNode p1 = head, p2 = head; while (p1 != null){ p1 = p1.next; k--;//p1先走k步,这样p1走到尾部,p2正好走到倒数第k个 if (k < 0) p2 = p2.next; } System.out.println(p2.val); } }
import java.io.*; class Node { int val; Node next; Node(int x) { val = x; next = null; } } public class Main{ public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); Node list1 = new Node(1); Node list2 = new Node(2); Node list3 = new Node(3); Node list4 = new Node(4); Node list5 = new Node(5); Node list6 = new Node(6); Node list7 = new Node(7); Node list = list1; list1.next = list2; list2.next = list3; list3.next = list4; list4.next = list5; list5.next = list6; list6.next = list7; while(list.next != null){ if(list.val == (8-n)){ System.out.println(list.val); return; } list = list.next; } System.out.println(list.val); } }
import java.util.*; class ListNode{ int val; ListNode next = null; ListNode(int val){ this.val=val; } } public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int k = sc.nextInt(); ListNode list = create(); for(int i=0;i<7-k;i++){ list=list.next; } System.out.print(list.val); } public static ListNode create(){ ListNode preList=new ListNode(0); ListNode list=preList; for(int i=0;i<7;i++){ list.next=new ListNode(i+1); list=list.next; } return preList.next; } }任意长度的链表倒数k个的方法。
import java.util.*; class ListNode{ int val; ListNode next = null; ListNode(int val){ this.val=val; } } public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int k = sc.nextInt(); ListNode list = create(); ListNode lowList = list; for(int i=0;i<k;i++){ list=list.next; } while(list!=null) { lowList=lowList.next; list = list.next; } System.out.print(lowList.val); } public static ListNode create(){ ListNode preList=new ListNode(0); ListNode list=preList; while(sc.hasNext()){ list.next=new ListNode(sc.nextInt()); list=list.next; } return preList.next; } }