Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

reverse linked list

#include <iostream>
using namespace std;

typedef struct node Node;

struct node{
    
    int data;
    Node *next;
};

Node *Create_Node(int data, Node *next){
    
    Node *new_node = (Node *) malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = next;
    return new_node;
}

Node *Insert_Node(Node *head, int data){
    
    Node *new_node = Create_Node(data, NULL);
    
    if(head == NULL){
        return new_node;
    }
    Node *current_node = head;
    while(current_node->next != NULL){
        
        current_node = current_node->next;
    }
    
    current_node->next = new_node;
    
    return head;
}

Node *Reverse_LinkedList(Node *head){
    
    Node *temp = NULL;
    Node *prev_data = NULL;
    Node *current_node = head;
    
    while(current_node != NULL){
        
        prev_data = current_node;
        current_node = current_node->next;
        prev_data->next = temp;
        temp = prev_data;
    }
    head = prev_data;
    return head;
}

void Print_LinkedList(Node *head){
    
    Node *current_node = head;
    while(current_node != NULL){
        printf("%d ",current_node->data);
        current_node = current_node->next;
    }
    printf("
");
}

int main() {
	
	Node *head;
	
	head = Insert_Node(head, 10);
	head = Insert_Node(head, 20);
	head = Insert_Node(head, 30);
	
	Print_LinkedList(head);
		
	head = Reverse_LinkedList(head);
	
	Print_LinkedList(head);
	return 0;
}
Comment

linked list reverse

//iterative
public ListNode ReverseList(ListNode head) 
{
    ListNode prev = null;
    var current= head;

    while(current!=null)
    {
        var next = current.next;
        current.next=prev;
        prev =current;
        current=next;
    }
    return prev;
}
// recursive
public ListNode ReverseList(ListNode head) 
{
    if(head==null || head.next == null)
        return head;

    var t2 = ReverseList(head.next);
    head.next.next = head;
    head.next = null;

    return t2;
}
Comment

Reverse Linked List

 public ListNode ReverseList(ListNode head) 
 {
   if(head==null || head.next == null)
   	return head;

  var t = ReverseList(head.next);
  head.next.next = head;
  head.next = null;

  return t;
}
Comment

reverse linked list

def reverseLinkedList(self):
        # Complexity Time: O(n), Complexity Memory: O(1)
        # Initialize previous and current pointers
        prev, curr = None, self.head
        # loop over the linked list nodes until the current is Null 
        while curr:
        	# Store current's next node in a temporary variable 
            next = curr.next
            # Reverse current node's pointer
            curr.next = prev
            # Move pointers one position ahead
            prev = curr
            curr = next
        self.head = prev
Comment

reverse a linkedlist

/*method to reverse a linked list */ 
reverse(list) {
            let current = list.head;
            let prev = null;
            let next = null;
            if (this.head) {
                //only one node
                if (!this.head.next) { return this; }
                while (current) {
                    next = current.next;//store next node of current before change
                    current.next = prev;//change next of current by reverse the link
                    prev = current;//move prev node forward
                    current = next;//move current node forward
                }
                list.head = prev
                return list
            }
            return "is empty"
        }
Comment

Reverse a Linked List

	    /* 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
Comment

reverse a linked list

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        
        ListNode current = head;
        ListNode prev = null;
        ListNode next = null;
        
        if(head == null)
        {
            return head;
        }
        while(current != null)
        {
            next = current.next;
            current.next = prev;
            prev = current;
            current = next;
        }
        return prev;
    }
}
Comment

Revese a Linked List

LinkedList<Integer> ll = new LinkedList<>();

ll.add(1);
ll.add(2);
ll.add(3);
System.out.println(ll);

LinkedList<Integer> ll1 = new LinkedList<>();

ll.descendingIterator().forEachRemaining(ll1::add);

System.out.println(ll1);
Comment

reverse linkedlist

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode prev = null;  
        ListNode current = head;
    
        
        while(current != null) { 
            ListNode next = current.next; 
            current.next = prev;
            prev = current;
            current = next;
        }
       return prev; 
    }
}
Comment

reverse linkedlist

Collections.reverse(list);
Comment

Reverse a Linked List

void reverse()
	{
		node* current, * prev, * temp;
		current = head;
		prev = NULL;
		while (current != NULL)
		{
			temp = current->next;
			current->next = prev;
			prev = current;
			current = temp;
		}
		head = prev;
	}
Comment

reverse linked list python

# Python program to reverse a linked list
# Time Complexity : O(n)
# Space Complexity : O(n) as 'next' 
#variable is getting created in each loop.
  
# Node class
  
  
class Node:
  
    # Constructor to initialize the node object
    def __init__(self, data):
        self.data = data
        self.next = None
  
  
class LinkedList:
  
    # Function to initialize head
    def __init__(self):
        self.head = None
  
    # Function to reverse the linked list
    def reverse(self):
        prev = None
        current = self.head
        while(current is not None):
            next = current.next
            current.next = prev
            prev = current
            current = next
        self.head = prev
  
    # Function to insert a new node at the beginning
    def push(self, new_data):
        new_node = Node(new_data)
        new_node.next = self.head
        self.head = new_node
  
    # Utility function to print the LinkedList
    def printList(self):
        temp = self.head
        while(temp):
            print (temp.data,end=" ")
            temp = temp.next
  
  
# Driver program to test above functions
llist = LinkedList()
llist.push(20)
llist.push(4)
llist.push(15)
llist.push(85)
  
print ("Given Linked List")
llist.printList()
llist.reverse()
print ("
Reversed Linked List")
llist.printList()
  
Comment

PREVIOUS NEXT
Code Example
Csharp :: remove item from list in for loop c# 
Csharp :: C# domain name to ip address 
Csharp :: Oculus Unity button press 
Csharp :: reload usercontol wpf 
Csharp :: new datetime c# 
Csharp :: mysql: [Warning] Using a password on the command line interface can be insecure. 
Csharp :: c# max function 
Csharp :: unity initialize array 
Csharp :: c# how to append in array 
Csharp :: longest substring without repeating characters c# 
Csharp :: c# method returns multiple values 
Csharp :: c# tuple 
Csharp :: finding keys in the registry 
Csharp :: vb.net center form in screen 
Csharp :: join dictionaries keys c# 
Csharp :: take photo function in unity 
Csharp :: c# find element in list of list 
Csharp :: c# $ string 
Csharp :: C# Convert 1 range to another 
Csharp :: photon2 addcalbacktarget 
Csharp :: c# window form align right bottom 
Csharp :: moq set delay to return 
Csharp :: convert getdate to ist c# 
Csharp :: unity apply bloom of a different color 
Csharp :: adding additional parameter to form submit 
Csharp :: unity find all scriptable objects of a type 
Csharp :: bind repeater to dictionary 
Csharp :: unity stop object from rotating 
Csharp :: how to set the current user httpcontext.current.user asp.net -mvc 
Csharp :: c# decimal to fixed 2 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =