首页 > 编程知识 正文

leetcode动态规划,leetcode 758 C语言

时间:2023-05-04 08:51:29 阅读:160735 作者:4706

你总是忘记翻转链表吗? 206 .链接列表的反转(1)递归(2)迭代141 )环形链接列表19 )删除链接列表中倒数第n个节点

206 .翻转链表

反转单链表。

样品:

输入: 1-2-3-4-5-NULL

输出: 5-4-3-2-1-NULL

)1)递归publiclistnodereverselist (listnode head ) if ) head==null ) return head; if(head.next==null ) return head; listnode last=reverse list (head.next ); head.next.next=head; head.next=null; 返回最后; () ) ) ) )。

)2)迭代publiclistnodereverselist (listnode head ) if ) head==空) return head; 列表节点b,c; c=头; 头=空; while(c!=null(b=c.next; //先取前面的位置c.next=head; 回头看标题//自己脑袋c=b; //继续}返回头; () ) ) ) )。

141 .类似链接列表的问题注意首尾

示例1 :

输入: head=[ 3,2,0,-4],pos=1

输出: true

解释:链表有一个循环,其末尾连接到第二个节点。

publicbooleanhascycle (listnode head ) if ) head==null||head.next==null ) return false; listnode man=头; ListNode kuai=head; //初始While(Kuai!=nullkuai.next!=null}{man=man.next; kuai=kuai.next.next; if(man==Kuai ) return true; } return false; } 19 .删除链表倒数第n个节点输入。 head=[ 1,2,3,4,5 ],n=2

输出: [ 1,2,3,5 ]

示例2 :

输入: head=[1],n=1

输出: []

示例3 :

输入: head=[ 1,2 ],n=1

输出: [1]

publiclistnoderemoventhfromend (listnode head,int n ) inta=0,c=0; listnodeaa=newlistnode(0,head ); ListNode b=aa; 头儿!=null () a; head=head.next; }for(intI=1; i a - n 1; I ) { b=b.next; } b.next=b.next.next; //如果记得头部节点,则该链表还没有消失的ListNode dummy=aa.next; 返回占空比; }如有错误,欢迎留言。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。