给定指向链表头节点的指针,任务是反转链表。我们需要通过更改节点之间的链接来反转列表。
例子:
输入:以下链接列表的
1-> 2-> 3-> 4-> NULL
输出:链接列表应更改为
4-> 3-> 2-> 1-> NULL输入:以下链接列表的
1-> 2-> 3-> 4-> 5-> NULL
输出:链接列表应更改为
5-> 4-> 3-> 2-> 1-> NULL输入:NULL
输出:NULL输入:1-> NULL
输出:1-> NULL
迭代法
- 将三个指针prev初始化为NULL,将curr初始化为head,将next初始化为NULL。
- 遍历链接列表。循环执行以下操作。
// //在更改下一个当前值之前,
//存储下一个节点
next = curr-> next
//现在更改下一个当前
// //这是实际反转发生的位置
curr-> next = prev
//将prev向前移动 一倍
prev = curr
curr =下一个
下面是上述方法的实现:
// Java program for reversing the linked list
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
/* Function to reverse the linked list */
Node reverse(Node node)
{
Node prev = null ;
Node current = node;
Node next = null ;
while (current != null ) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
node = prev;
return node;
}
// prints content of double linked list
void printList(Node node)
{
while (node != null ) {
System.out.print(node.data + " " );
node = node.next;
}
}
// Driver Code
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node( 85 );
list.head.next = new Node( 15 );
list.head.next.next = new Node( 4 );
list.head.next.next.next = new Node( 20 );
System.out.println( "Given Linked list" );
list.printList(head);
head = list.reverse(head);
System.out.println( "" );
System.out.println( "Reversed linked list " );
list.printList(head);
}
}
// This code has been contributed by Mayank Jaiswal
|
输出:
给定链表
85 15 4 20
反向链接列表
20 4 15 85
时间复杂度: O(n)
空间复杂度: O(1)
递归方法:
1)将清单分为两部分-第一个节点和
链表的其余部分。
2)调用反向链接列表的其余部分。
3)将休息链接到第一位。
4)固定头指针
// Recursive Java program to reverse
// a linked list
class recursion {
static Node head; // head of list
static class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
static Node reverse(Node head)
{
if (head == null || head.next == null )
return head;
/* reverse the rest list and put
the first element at the end */
Node rest = reverse(head.next);
head.next.next = head;
/* tricky step -- see the diagram */
head.next = null ;
/* fix the head pointer */
return rest;
}
/* Function to print linked list */
static void print()
{
Node temp = head;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
System.out.println();
}
static void push( int data)
{
Node temp = new Node(data);
temp.next = head;
head = temp;
}
/* Driver program to test above function*/
public static void main(String args[])
{
/* Start with the empty list */
push( 20 );
push( 4 );
push( 15 );
push( 85 );
System.out.println( "Given linked list" );
print();
head = reverse(head);
System.out.println( "Reversed Linked list" );
print();
}
}
// This code is contributed by Prakhar Agarwal
|
输出:
给定链表
85 15 4 20
反向链接列表
20 4 15 85
时间复杂度: O(n)
空间复杂度: O(1)
一种更简单的尾递归方法
下面是此方法的实现。
// Java program for reversing the Linked list
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
// A simple and tail recursive function to reverse
// a linked list. prev is passed as NULL initially.
Node reverseUtil(Node curr, Node prev)
{
/*If head is initially null OR list is empty*/
if (head == null )
return head;
/* If last node mark it head*/
if (curr.next == null ) {
head = curr;
/* Update next to prev node */
curr.next = prev;
return head;
}
/* Save curr->next node for recursive call */
Node next1 = curr.next;
/* and update next ..*/
curr.next = prev;
reverseUtil(next1, curr);
return head;
}
// prints content of double linked list
void printList(Node node)
{
while (node != null ) {
System.out.print(node.data + " " );
node = node.next;
}
}
// Driver Code
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node( 1 );
list.head.next = new Node( 2 );
list.head.next.next = new Node( 3 );
list.head.next.next.next = new Node( 4 );
list.head.next.next.next.next = new Node( 5 );
list.head.next.next.next.next.next = new Node( 6 );
list.head.next.next.next.next.next.next
= new Node( 7 );
list.head.next.next.next.next.next.next.next
= new Node( 8 );
System.out.println( "Original Linked list " );
list.printList(head);
Node res = list.reverseUtil(head, null );
System.out.println( "" );
System.out.println( "" );
System.out.println( "Reversed linked list " );
list.printList(res);
}
}
// This code has been contributed by Mayank Jaiswal
|
输出量
给定链表
1 2 3 4 5 6 7 8
反向链表
8 7 6 5 4 3 2 1
使用堆栈:
- 将节点(值和地址)存储在堆栈中,直到输入所有值。
- 完成所有输入后,将Head指针更新到最后一个位置(即最后一个值)。
- 开始弹出节点(值和地址)并以相同顺序存储它们,直到堆栈为空。
- 用NULL更新堆栈中最后一个节点的下一个指针。
// Java program for above approach
import java.util.*;
class GFG{
// Create a class Node to enter
// values and address in the list
static class Node
{
int data;
Node next;
};
static Node head = null ;
// Function to reverse the
// linked list
static void reverseLL()
{
// Create a stack "s"
// of Node type
Stack<Node> s = new Stack<>();
Node temp = head;
while (temp.next != null )
{
// Push all the nodes
// in to stack
s.add(temp);
temp = temp.next;
}
head = temp;
while (!s.isEmpty())
{
// Store the top value of
// stack in list
temp.next = s.peek();
// Pop the value from stack
s.pop();
// update the next pointer in the
// in the list
temp = temp.next;
}
temp.next = null ;
}
// Function to Display
// the elements in List
static void printlist(Node temp)
{
while (temp != null )
{
System.out.print(temp.data+ " " );
temp = temp.next;
}
}
// Program to insert back of the
// linked list
static void insert_back( int value)
{
// we have used insertion at back method
// to enter values in the list.(eg:
// head.1.2.3.4.Null)
Node temp = new Node();
temp.data = value;
temp.next = null ;
// If *head equals to null
if (head == null )
{
head = temp;
return ;
}
else
{
Node last_node = head;
while (last_node.next != null )
{
last_node = last_node.next;
}
last_node.next = temp;
return ;
}
}
// Driver Code
public static void main(String[] args)
{
insert_back( 1 );
insert_back( 2 );
insert_back( 3 );
insert_back( 4 );
System.out.print( "Given linked list\n" );
printlist(head);
reverseLL();
System.out.print( "\nReversed linked list\n" );
printlist(head);
}
}
// This codeis contributed by gauravrajput1
|
输出量
给定链表
1 2 3 4
反向链表
4 3 2 1