reverse a linked list
/*
public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; next = null; }
}
*/
public static ListNode[] reverse_linked_list(ListNode head) {
ListNode prev = null;
ListNode current = head;
ListNode next;
ListNode tail = head;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
ListNode[] result = {head, tail};
return result;
}
4.25
4
/* Before changing next pointer of current node,
store the next node */
next = curr -> next
/* Change next pointer of current node */
/* Actual reversing */
curr -> next = prev
/* Move prev and curr one step ahead */
prev = curr
curr = next
Thank you!
4
0
4.63
8
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
Thank you!
8
0
0
0
Collections.reverse(list);
Thank you!
0
0
3.6
5
#include <stdio.h>
struct Node {
int data;
struct Node* next;
Node(int data){
this->data = data;
next = NULL;
}
};
struct LinkedList {
Node* head;
LinkedList(){
head = NULL;
}
void interReverseLL(){
Node* current = head;
Node *prev = NULL, *after = NULL;
while (current != NULL) {
after = current->next;
current->next = prev;
prev = current;
current = after;
}
head = prev;
}
void print() {
struct Node* temp = head;
while (temp != NULL) {
printf("%d ", temp-> data);
temp = temp->next;
}
printf("\n");
}
void push(int data){
Node* temp = new Node(data);
temp->next = head;
head = temp;
}
};
int main() {
LinkedList linkedlist;
linkedlist.push(85);
linkedlist.push(10);
linkedlist.push(65);
linkedlist.push(32);
linkedlist.push(9);
printf("Linked List : \t");
linkedlist.print();
linkedlist.interReverseLL();
printf("Reverse Linked List : \t");
linkedlist.print();
return 0;
}
Output
Linked List : 9 32 65 10 85
Reverse Linked List : 85 10 65 32 9
Thank you!
5
0
4
5
#include<bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node *next;
};
// To create a demo we have to construct a linked list and this
// function is to push the elements to the list.
void push(struct node **head_ref, int data) {
struct node *node;
node = (struct node*)malloc(sizeof(struct node));
node->data = data;
node->next = (*head_ref);
(*head_ref) = node;
}
// Function to reverse the list
void reverse(struct node **head_ref) {
struct node *temp = NULL;
struct node *prev = NULL;
struct node *current = (*head_ref);
while(current != NULL) {
temp = current->next;
current->next = prev;
prev = current;
current = temp;
}
(*head_ref) = prev;
}
// To check our program
void printnodes(struct node *head) {
while(head != NULL) {
cout<<head->data<<" ";
head = head->next;
}
}
// Driver function
int main() {
struct node *head = NULL;
push(&head, 0);
push(&head, 1);
push(&head, 8);
push(&head, 0);
push(&head, 4);
push(&head, 10);
cout << "Linked List Before Reversing" << endl;
printnodes(head);
reverse(&head);
cout << endl;
cout << "Linked List After Reversing"<<endl;
printnodes(head);
return 0;
}
Thank you!
5
0
Are there any code examples left?
New code examples in category Other
-
Other 2023-03-27 22:50:10 how to select the whole line in vscode with keyboard shortcut
-
Other 2022-03-27 22:45:24 income of a web developer
-
Other 2022-03-27 22:35:01 \pyrcc_main.py: File does not exist 'resources.qrc'
-
Other 2022-03-27 22:30:45 rick roll embed code
-
Other 2022-03-27 22:20:08 Circuit_04_Potentiometer
-
Other 2022-03-27 22:20:05 iterative power
-
Other 2022-03-27 22:15:11 flutter run all
-
Other 2022-03-27 22:10:05 when is karlson release
-
Other 2022-03-27 22:10:02 wp .htaccess example
-
Other 2022-03-27 22:00:08 bash pause in file read line by line