核心代码:
public static void List_Inverse(ListNode h) { ListNode p = h.getNext(), q; while (p.next != null) { q = p.next; p.next = q.next; q.next = h.next; h.next = q; } }
然后可能前几天比较空,做了个可视化界面(普普通通,没什么特色):
附个代码,造福后来人
package list_inverse.java; import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.JLabel; import java.awt.Font; import javax.swing.JTextField; import javax.swing.JScrollPane; import javax.swing.JButton; import javax.swing.JTable; import javax.swing.table.DefaultTableModel; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class View extends JFrame { public static class ListNode { String value; ListNode next; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public ListNode getNext() { return next; } public void setNext(ListNode next) { this.next = next; } ListNode(String x) { value = x; next = null; } } private JPanel contentPane; private JTextField tf_inpute; private JTable table; private JButton btnNewButton_1; static int row; static String val; static ListNode head = new ListNode(""); ListNode p = head; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { View frame = new View(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public View() { setTitle("单链表就地反转"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 700, 600); this.setLocationRelativeTo(null);// 居中 contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); JLabel lblNewLabel = new JLabel("输入结点元素:"); lblNewLabel.setFont(new Font("楷体", Font.PLAIN, 20)); lblNewLabel.setBounds(82, 47, 170, 38); contentPane.add(lblNewLabel); tf_inpute = new JTextField(); tf_inpute.setBounds(239, 50, 178, 38); contentPane.add(tf_inpute); tf_inpute.setColumns(10); JScrollPane scrollPane = new JScrollPane(); scrollPane.setBounds(82, 110, 467, 339); contentPane.add(scrollPane); table = new JTable(); table.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { row = table.getSelectedRow(); val = table.getValueAt(row, 0).toString(); } }); table.setModel(new DefaultTableModel(new Object[][] {}, new String[] { "\u7ED3\u70B9\u6570\u503C" })); scrollPane.setViewportView(table); JButton btnNewButton = new JButton("reverse"); btnNewButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { List_Inverse(head); fillTable(); } }); btnNewButton.setFont(new Font("楷体", Font.PLAIN, 20)); btnNewButton.setBounds(407, 478, 141, 44); contentPane.add(btnNewButton); btnNewButton_1 = new JButton("提交"); btnNewButton_1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { p = head; while (p.getNext() != null) { p = p.getNext(); } String node = tf_inpute.getText(); p.next = new ListNode(node); p = p.next; fillTable(); tf_inpute.setText(""); } }); btnNewButton_1.setFont(new Font("楷体", Font.PLAIN, 20)); btnNewButton_1.setBounds(449, 50, 100, 33); contentPane.add(btnNewButton_1); JButton btnNewButton_2 = new JButton("删除"); btnNewButton_2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { p = head; while (p.getNext() != null && !p.getNext().getValue().equals(val)) { p = p.getNext(); } p.setNext(p.getNext().getNext()); fillTable(); } }); btnNewButton_2.setFont(new Font("楷体", Font.PLAIN, 20)); btnNewButton_2.setBounds(105, 481, 107, 38); contentPane.add(btnNewButton_2); JButton btnNewButton_3 = new JButton("清空"); btnNewButton_3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { p = head.getNext(); head.setNext(null); while (p != null) { ListNode q = p; p = p.getNext(); q = null; } fillTable(); } }); btnNewButton_3.setFont(new Font("楷体", Font.PLAIN, 20)); btnNewButton_3.setBounds(260, 484, 100, 33); contentPane.add(btnNewButton_3); } public void fillTable() { DefaultTableModel defaultTable = (DefaultTableModel) table.getModel(); defaultTable.setRowCount(0); p = head.getNext(); while (p != null) { defaultTable.addRow(new Object[] { p.getValue() }); p = p.getNext(); } } public static void List_Inverse(ListNode h) { ListNode p = h.getNext(), q; while (p.next != null) { q = p.next; p.next = q.next; q.next = h.next; h.next = q; } } }