链表之单、双链表反序
生活随笔
收集整理的這篇文章主要介紹了
链表之单、双链表反序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個單鏈表,然后對它反序。
ListNode類
public class ListNode {int val;ListNode next;ListNode(int x){val=x;next=null;}ListNode(){}ListNode(int x,ListNode node){val=x;next=node;}public void setVal(int val){this.val=val;}public int getVal(){return val;}public void setListNode(ListNode next){this.next=next;}public ListNode getListNode(){return next;}//單鏈表反序public ListNode reverse(ListNode head){ListNode pre=null;ListNode next=null;while(head!=null){next=head.next;head.next=pre;pre=head;head=next;}return pre;}}
給定一個雙鏈表反序
總結
以上是生活随笔為你收集整理的链表之单、双链表反序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode之Add Two Num
- 下一篇: 用beyond compare解决git