基础大杂烩 -- 目录

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

1.输入并查找

  方案:头插法,正向查找第m个元素。

import java.util.Scanner;
 
public class Main {
 
    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            ListNode node = new ListNode();
            node.next = null;
            int N = in.nextInt();
             
            for (int i = 0; i < N; i++) {
                ListNode p = new ListNode();
                int x = in.nextInt();
                p.next = node.next;
                p.data = x;
                node.next = p;
            }
            int k = in.nextInt();
            ListNode kthNode = getKthNode(node,k);
            System.out.println(kthNode.data);
        }
    }
     
    public static ListNode getKthNode(ListNode node,int k){
        ListNode front = node,behind = node;
        int x=0;
        while(front.next!=null && x<=k){
            x++;
            front = front.next;
        }
        return front;
    }
}
 
class ListNode {
    public int data;
    public ListNode next;
}

2.指定单向链表并查找倒数第m个元素

  两种情况:无环、有环

    无环方案:因为无环单向链表最后一个元素next一定为null,因此设置两个指针slow = head ,fast = head + m,当fast.next == null时slow.next恰好是倒数第m个元素。

    有环方案:查找连接点,分割成无环单向链表,最后一个元素next一定为连接点join,将最后一个元素next --> null,此时用无环方案即可解决。因此设置连个指针slow = head ,fast = head + m,当fast.next == null时slow.next恰好是倒数第m个元素。

  无环方案Class :

package limeMianShi.link;

public class Test01 {

    public static void main(String[] args) {
ListNode head = new ListNode(0);
ListNode p = head;
for (int i = 1; i <= 10; i++) {
ListNode node = new ListNode(i);
p.next = node;
p = node; }
printListNode(head);
printKElement(head,3);
} private static void printKElement(ListNode head, int m) {
if(m == 0){
System.out.println("null");
}
ListNode p = head;
if(m > 0){
for(int i = 0;i < m;i++){
if(i == m - 1){
System.out.println("正数第" + m + "个元素为: " + p.data);
}
p = p.next;
}
}else{
ListNode s = head;
for(int i = 0;i < -m;i++){
p = p.next;
}
while(null != p){
s = s.next;
p = p.next;
}
System.out.println("倒数第" + (-m) + "个元素为: " + s.data);
}
} private static void printListNode(ListNode head) {
if (null != head) {
System.out.print(head.data + " --> ");
printListNode(head.next);
}else{
System.out.println("null");
}
}
} class ListNode { public int data;
public ListNode next; public ListNode() {
super();
} public ListNode(int data) {
this.data = data;
}
}

  有环方案Class :

package limeMianShi.link;

public class Test02 {

    public static void main(String[] args) {
ListNode head = new ListNode(0);
ListNode p = head;
ListNode p4 = null;
for (int i = 1; i <= 10; i++) {
ListNode node = new ListNode(i);
p.next = node;
p = node;
if (i == 4) {
p4 = node;
}
}
p.next = p4;
pullLoop(head);
printListNode(head);
for (int i = 1; i < 11; i++) {
printKElement(head, -i);
}
} private static void printListNode(ListNode head) {
if (null != head) {
System.out.print(head.data + " --> ");
printListNode(head.next);
} else {
System.out.println("null");
}
} private static void printKElement(ListNode head, int m) {
if (m == 0) {
System.out.println("null");
}
ListNode p = head;
if (m > 0) {
for (int i = 0; i < m; i++) {
if (i == m - 1) {
System.out.println("正数第" + m + "个元素为: " + p.data);
}
p = p.next;
}
} else {
ListNode s = head;
for (int i = 0; i < -m; i++) {
p = p.next;
}
while (null != p) {
s = s.next;
p = p.next;
}
System.out.println("倒数第" + (-m) + "个元素为: " + s.data);
}
} private static void pullLoop(ListNode head) {
ListNode p = getP(head);
ListNode pre = null;
while (head != p) {
pre = p;
head = head.next;
p = p.next;
}
pre.next = null;
} private static ListNode getP(ListNode head) {
ListNode solw = head.next;
ListNode fast = head.next.next;
while (solw != fast) {
solw = solw.next;
fast = fast.next.next;
}
return solw;
}
} class ListNode {
public int data;
public ListNode next; public ListNode() {
} public ListNode(int data) {
this.data = data;
}
}

啦啦啦

大杂烩 -- 查找单向链表倒数第m个元素的更多相关文章

  1. 笔试题&amp;面试题:设计一个复杂度为n的算法找到单向链表倒数第m个元素

    设计一个复杂度为n的算法找到单向链表倒数第m个元素.最后一个元素假定是倒数第0个. 提示:双指针查找 相对于双向链表来说,单向链表仅仅能从头到尾依次訪问链表的各个节点,所以假设要找链表的倒数第m个元素 ...

  2. 求链表倒数第n个元素

    提示:设置一前一后两个指针,一个指针步长为1,另一个指针步长为n,当一个指针走到链表尾端时, 另一指针指向的元素即为链表倒数第n个元素. #include <stdio.h> #inclu ...

  3. 寻找链表倒数第k个元素,只遍历一遍(编程之美)

    class LNode { public LNode next; public int data; } /*找出倒数第k个元素,只遍历一遍*/ class Kk { private static LN ...

  4. 【剑指offer】输出链表倒数第K个元素

    /* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ ...

  5. C#学习单向链表和接口 IList<T>

    C#学习单向链表和接口 IList<T> 作者:乌龙哈里 时间:2015-11-04 平台:Window7 64bit,Visual Studio Community 2015 参考: M ...

  6. 输出单向链表中倒数第k个结点

    描述 输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第0个结点为链表的尾指针. 链表结点定义如下: struct ListNode { int       m_nKey; ListNode* ...

  7. 输入一个单向链表,输出该链表中倒数第K个结点

    输入一个单向链表,输出该链表中倒数第K个结点,具体实现如下: #include <iostream> using namespace std; struct LinkNode { publ ...

  8. 【编程题目】输入一个单向链表,输出该链表中倒数第 k 个结点

    第 13 题(链表):题目:输入一个单向链表,输出该链表中倒数第 k 个结点.链表的倒数第 0 个结点为链表的尾指针.链表结点定义如下: struct ListNode {int m_nKey;Lis ...

  9. 寻找单向链表的倒数第k个节点

    题目: 输入一个单向链表,输出这个单向链表的倒数第k个节点 template<class T> class ListNode { public: T Data; ListNode<T ...

随机推荐

  1. C++ 代码格式化工具Astyle

    1.下载Asyle程序. win版本:https://sourceforge.net/projects/astyle/ 2.将bin/AStyle.exe拷到源码目录中,在命令行终端执行. AStyl ...

  2. Web App 和 Native App,哪个是趋势?

    一.Web App vs. Native App 比起手机App,网站有一些明显的优点. 跨平台:所有系统都能运行 免安装:打开浏览器,就能使用 快速部署:升级只需在服务器更新代码 超链接:可以与其他 ...

  3. 混沌的艺术--- YChaos通过数学公式生成混沌图像

    艺术真得很难吗?也许如同编程一样容易.我写了一套软件,其功能是通过输入数学方程式,生成艺术图像.一提到数学有人可能会发怵,这里请不要担心,生成混沌的数学公式大都很是简单,基本上只用加.减.乘.除.余. ...

  4. 加密入门(三):TrueCrypt(转)

    http://terrychen.info/encryption-truecrypt/ TrueCrypt 是一款功能强大的开源加密工具,利用 TrueCrypt 可以创建一个加密文件作为虚拟加密卷, ...

  5. SpringBoot中配置起动时的数据库初始化角本

    一.简介 我们使用SpringBoot + JPA时,需要程序在启动时执行数据表的初始化或者数据库记录的初始化.一般数据表的初始化可以通过在Spring Boot的application.proper ...

  6. 在Linux平台上搭建EasyDarwin,编译代码并简单部署

    測试环境: Ubuntu gcc / g++ 从https://github.com/EasyDarwin/EasyDarwin下载代码 1.编译 第一步:进入源码文件夹下 cd ./EasyDarw ...

  7. python2判断编码格式

    def getCoding(strInput): ''' 获取编码格式 ''' if isinstance(strInput, unicode): return "unicode" ...

  8. logstash 学习小记

    logstash 学习小记 标签(空格分隔): 日志收集 Introduce Logstash is a tool for managing events and logs. You can use ...

  9. Authentication 方案优化探索(JWT, Session, Refresh Token, etc.)

    转载自:http://www.jianshu.com/p/5ac8a0e1e5a8

  10. php static 变量的例子

    class test { public static function a(){} public function b(){} } $obj = new test; 调用 代码 test::a(); ...