首页 > 其他分享 >链表

链表

时间:2023-11-22 16:33:50浏览次数:29  
标签:pre head ListNode cur next 链表

链表逆置

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode ReverseList (ListNode head) {
        // write code here
        ListNode pre = null;
        ListNode cur = head;
        while(cur!=null){
            ListNode next= cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}

链表有环

https://www.cnblogs.com/chenyi502/p/17494929.html

标签:pre,head,ListNode,cur,next,链表
From: https://www.cnblogs.com/chenyi502/p/17849635.html

相关文章

  • 单链表(SingleLinkedList)
    单链表1.创建一个Node类//head不能动,头节点作用是表示链表的头privateNodehead;//在linkedList类写一个Node的成员内部类privateclassNode{privateintdata;privateNodenext;publicNode(intdata){this.data=data;th......
  • C语言数据结构_查找并删除单链表中最大值结点并返回值
    代码实现1#include<stdio.h>2#include<stdlib.h>34typedefstructNode//定义一个结构体5{6floatdata;7structNode*next;8}Node;910Node*Chuangzao_LinkedList()//创建一个链表11{12Node*head=NULL;//......
  • 面试必刷TOP101:30、二叉搜索树与双向链表
    题目题解/*思路:首先根节点以及其左右子树,左子树的左子树和右子树的右子树相同*左子树的右子树和右子树的左子树相同即可,采用递归*非递归也可,采用栈或队列存取各级子树根节点*/publicclassSolution{ booleanisSymmetrical(TreeNodepRoot) { if(pRoot==null){ re......
  • 11.17双向循环链表应用
     #include<bits/stdc++.h>usingnamespacestd;typedefstructf{intdata;f*prior;f*next;}node,*Node;voidbuild(Nodep){intn;cin>>n;while(n--){intx;cin>>x;Nodenow=newnode()......
  • 11.15链表逆置
     structListNode*reverse(structListNode*head){structListNode*L=(structListNode*)malloc(sizeof(structListNode)),*p,*q;L->next=NULL;p=head;//中间量while(p){q=(structListNode*)malloc(sizeof(structListNode));......
  • (链表)21-分隔链表
    /***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*ListNode(){}*ListNode(intval){this.val=val;}*ListNode(intval,ListNodenext){this.val=val;this.next=next;}*}......
  • 面试题 02.07. 链表相交
    2023-11-21面试题02.07.链表相交-力扣(LeetCode)思路:1暴力法:判断的是next是不是相等1hashmap存储其中一个的全部,遍历另一个看是不是在map中(用set就行,不用map)2双指针:用2个指针分别遍历2链表(都是遍历完一个继续遍历另一个),最终会相等的,相等就是找到了暴力法:/***Defi......
  • 142. 环形链表 II
    2023-11-21142.环形链表II-力扣(LeetCode)思路:1hashmap:将其next一个个存入,直到找到next已经存在的,这里用set就行2快慢指针,一个一步步走,一个一次走2步,自己画一下图,其一定会在环中相遇,且一定是多走一圈,然后相遇时,将慢指针保留,继续走,重新定义一个指针从一开始走,他们相等时就......
  • 单链表建表,倒置,遍历(不使用Class,简洁易懂)
    在C++中通过递归方法实现单链表倒置初始化列表structListNode{ intval; LiseNode*next; ListNode(intx):val(x),next(NULL){}};遍历voidquery_node(){ node*p=head; while(p!=NULL){ cout<<p->data<<''; p=p->nxt; } cout<<endl;}建表(......
  • (链表)20-旋转链表
    1/**2*Definitionforsingly-linkedlist.3*publicclassListNode{4*intval;5*ListNodenext;6*ListNode(){}7*ListNode(intval){this.val=val;}8*ListNode(intval,ListNodenext){this.val=val;......