国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

牛客-劍指offer-單鏈表的反轉

2019-11-08 03:27:31
字體:
來源:轉載
供稿:網友

題目: 將一個鏈表進行反轉.

ListNode.java

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

Solution.java

/** * Created by xizwu on 2017/2/16. */public class Solution { public ListNode ReverseList(ListNode head) { if(head==null) return null; ListNode PRe=null; ListNode next=null; while(head !=null){ next=head.next; head.next=pre; pre=head; head=next; } return pre; } public ListNode insert(ListNode head,int val){ //1. 指向 鏈表的末尾 ListNode before=head; if(head==null){ before=new ListNode(val); return before; } while(head.next !=null){ head=head.next; } ListNode myNode=new ListNode(val); head.next=myNode; //2. 添加一個node return before; } public void myprint(ListNode head){ if(head==null){ System.out.println(" The list is empty"); return; } while(head!=null){ System.out.print(head.val+" "); head=head.next; } }}

maintest.java

/** * Created by xizwu on 2017/2/16. */public class maintest { public static void main(String[] args) { Solution myso = new Solution(); ListNode myhead = null; ListNode afterResverHead=null; myhead=myso.insert(myhead, 1); myhead=myso.insert(myhead, 2); myhead=myso.insert(myhead, 3); myso.myprint(myhead); afterResverHead=myso.ReverseList(myhead); System.out.println("#################"); myso.myprint(afterResverHead); }}

最后的結果:

1 2 3 ################# 3 2 1


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 重庆市| 泊头市| 永福县| 巨鹿县| 睢宁县| 武平县| 四川省| 石屏县| 陵川县| 盐亭县| 永川市| 栖霞市| 蛟河市| 嫩江县| 衡水市| 尼勒克县| 会昌县| 陇南市| 嫩江县| 鄂伦春自治旗| 元江| 灌阳县| 德钦县| 邻水| 宁蒗| 安远县| 双流县| 瑞金市| 秀山| 延寿县| 靖宇县| 富源县| 洮南市| 宜宾县| 新营市| 体育| 九江市| 西华县| 永清县| 太康县| 民乐县|