溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用Java實現(xiàn)單鏈表的反轉

發(fā)布時間:2022-02-21 17:13:55 來源:億速云 閱讀:154 作者:iii 欄目:開發(fā)技術

本篇內容主要講解“怎么使用Java實現(xiàn)單鏈表的反轉”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“怎么使用Java實現(xiàn)單鏈表的反轉”吧!

一、原地反轉

1、新建一個哨兵節(jié)點下一結點指向頭結點

2、把待反轉鏈表的下一節(jié)點插入到哨兵節(jié)點的下一節(jié)點

反轉之前的鏈表:1–>2–>3–>4>–>5

加入哨兵節(jié)點:dummp–>1–>2–>3–>4>–>5

原地反轉:

定義:prev=dummp.next; pcur=prev.next;

prev.next=pcur.next;

pcur.next=dummp.next;

dummp.next=pcur;

pcur=prev.next;

怎么使用Java實現(xiàn)單鏈表的反轉

怎么使用Java實現(xiàn)單鏈表的反轉

public Stu_node reverse_list(Stu_node head){
        if (head.next==null ||head.next.next==null)
            return null;
        Stu_node dump = new Stu_node(-1," ");
        dump.next=head;
        Stu_node prev = dump.next;
        Stu_node pcur = prev.next;
        while(pcur!=null){
            prev.next=pcur.next;
            pcur.next=dump.next;
            dump.next=pcur;
            pcur=prev.next;
        }
        return dump.next;
    }

二、新建鏈表頭結點插法

二、新建鏈表頭結點插法:

新建一個頭結點,遍歷原鏈表,把每個節(jié)點用頭結點插入到新建鏈表中。最后,新建的鏈表就是反轉后的鏈表。

public Stu_node reverse_list1 (Stu_node head){
        //新建一個新的鏈表的頭結點
        Stu_node dump = new Stu_node(-1," ");
        Stu_node pcur = head;
        //遍歷待反轉鏈表,頭結點插入到新的鏈表中
        while(pcur!=null){
            Stu_node pnext = pcur.next;
            pcur.next = dump.next;
            dump.next=pcur;
            pcur=pnext;
        }
        //新鏈表頭結點不是需要返回的數(shù)據(jù),因此返回頭結點的下一節(jié)點
        return dump.next;
    }

三、利用棧結構實現(xiàn)鏈表的反轉

由于棧結構存儲數(shù)據(jù)是先進后出(后進先出)也可以通過棧達到反轉鏈表的目的。

 public Stu_node reverse_stack(Stu_node head){
        Stack<Stu_node> stack = new Stack<>();
        Stu_node temp = head;
        //鏈表入棧
        while(temp!=null){
            stack.push(temp);
            temp=temp.next;
        }
        //取出棧中的一個節(jié)點當做新的鏈表的頭結點
        Stu_node new_head = stack.pop();
        Stu_node cur = new_head;
        //出站
        while(!stack.isEmpty()){
            Stu_node node = stack.pop();
            //將出站的節(jié)點指向取消
            node.next=null;
            //將新的鏈表串起來
            cur.next = node;
            cur = node;
        }
        return new_head;
    }

四、完整代碼奉上

import java.util.Stack;

public class revere_node {
    public static void main(String[] args) {
        LinkedNode list= new LinkedNode();
        Stu_node node1 = new Stu_node(1,"張三");
        Stu_node node2 = new Stu_node(2,"李四");
        Stu_node node3 = new Stu_node(3,"王二");
        Stu_node node4 = new Stu_node(4,"麻子");
        Stu_node node5 = new Stu_node(5,"趙六");
        //打印添加節(jié)點之前的鏈表
        list.print();
        //尾結點添加節(jié)點
        list.add(node1);
        list.add(node2);
        list.add(node3);
        list.add(node4);
        list.add(node5);
        //打印添加加點之后的鏈表
        list.print();
        System.out.println("-------------------");
        //定義一個頭結點接收調用函數(shù)返回的頭節(jié)點
        Stu_node head = list.reverse_stack(list.head);
        //遍歷輸出每個節(jié)點
        while (head.next!=null){
            System.out.println(head);
            head=head.next;
        }

    }
}
//定義一個鏈表的操作類
class LinkedNode{
    //定義一個頭結點
    Stu_node head = new Stu_node(-1," ");
    //添加鏈表的方法
    public void add(Stu_node node){
        Stu_node temp = head;
        while(true){
            if (temp.next==null)
                break;
            temp=temp.next;
        }
        temp.next=node;
    }
    //打印鏈表
    public void print(){
        Stu_node temp = head.next;
        if (head.next==null){
            System.out.println("此鏈表為空");
        }
        while (temp!=null){
            System.out.println(temp);
            temp=temp.next;
        }
    }
    //原地反轉
    public Stu_node reverse_list(Stu_node head){
        if (head.next==null ||head.next.next==null)
            return null;
        Stu_node dump = new Stu_node(-1," ");
        dump.next=head;
        Stu_node prev = dump.next;
        Stu_node pcur = prev.next;
        while(pcur!=null){
            prev.next=pcur.next;
            pcur.next=dump.next;
            dump.next=pcur;
            pcur=prev.next;
        }
        return dump.next;
    }
    //新建一個新的鏈表,頭結點插入法實現(xiàn)鏈表的反轉
    public Stu_node reverse_list1 (Stu_node head){
        Stu_node dump = new Stu_node(-1," ");
        Stu_node pcur = head;
        while(pcur!=null){
            Stu_node pnext = pcur.next;
            pcur.next = dump.next;
            dump.next=pcur;
            pcur=pnext;
        }
        return dump.next;
    }
    //利用棧實現(xiàn)反轉鏈表
    public Stu_node reverse_stack(Stu_node head){
        Stack<Stu_node> stack = new Stack<>();
        Stu_node temp = head;
        //鏈表入棧
        while(temp!=null){
            stack.push(temp);
            temp=temp.next;
        }
        //取出一個節(jié)點當做新的鏈表的頭結點
        Stu_node new_head = stack.pop();
        Stu_node cur = new_head;
        //出站
        while(!stack.isEmpty()){
            Stu_node node = stack.pop();
            //將出站的節(jié)點指向取消
            node.next=null;
            //將新的鏈表串起來
            cur.next = node;
            cur = node;
        }
        return new_head;
    }
}
//節(jié)點類
class Stu_node{
    int num;
    String name;
    Stu_node next;
    //重寫toString方法,顯示節(jié)點數(shù)據(jù)
    @Override
    public String toString() {
        return "Stu_node{" +
                "num=" + num +
                ", name='" + name + ''' +
                '}';
    }

    public Stu_node(int num, String name) {
        this.num = num;
        this.name = name;
    }
}

到此,相信大家對“怎么使用Java實現(xiàn)單鏈表的反轉”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI