Android में लिंक्ड लिस्ट को रिवर्स करना
यह उदाहरण दर्शाता है कि Android में लिंक्ड लिस्ट को कैसे रिवर्स (उल्टा) किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और आवश्यक विवरण भरकर एक नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट XML में कोड जोड़ें
res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:gravity="center"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/text"
android:textSize="30sp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
उपरोक्त कोड में, हमने लिंक्ड लिस्ट के रिवर्स को दिखाने के लिए TextView लिया है।
चरण 3: MainActivity.java में कोड जोड़ें
package com.example.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
public class MainActivity extends AppCompatActivity {
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.add(2);
linkedList.add(8);
linkedList.add(2);
linkedList.add(5);
linkedList.add(7);
linkedList.add(5);
linkedList.add(9);
// 1. सॉर्ट करें
Collections.sort(linkedList); // [2, 2, 5, 5, 7, 8, 9]
// 2. रिवर्स करें
Collections.reverse(linkedList); // [9, 8, 7, 5, 5, 2, 2]
// 3. डुप्लिकेट हटाएं (LinkedHashSet ऑर्डर बनाए रखता है)
LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(linkedList);
TextView textView = findViewById(R.id.text);
textView.setText("रिवर्स और यूनिक: " + hashSet.toString());
}
}
एप्लिकेशन चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और परिणाम देखें।

परिणाम: [9, 8, 7, 5, 2] - सॉर्टेड, रिवर्स्ड, और डुप्लिकेट-मुक्त।
लिंक्ड लिस्ट रिवर्स करने के तरीके
1. Collections.reverse() - सबसे सरल
LinkedList<Integer> list = new LinkedList<>(); list.add(1); list.add(2); list.add(3); Collections.reverse(list); // [3, 2, 1]
2. Iterator का उपयोग करके (मैन्युअल)
public static <E> void reverseManually(LinkedList<E> list) {
for (int i = 0, j = list.size() - 1; i < j; i++, j--) {
E temp = list.get(i);
list.set(i, list.get(j));
list.set(j, temp);
}
}
3. कस्टम Node क्लास के साथ (इंटरव्यू स्टाइल)
class Node {
int data;
Node next;
Node(int data) { this.data = data; }
}
public Node reverseList(Node head) {
Node prev = null;
Node current = head;
Node next = null;
while (current != null) {
next = current.next; // अगला नोड सेव करें
current.next = prev; // लिंक रिवर्स करें
prev = current; // prev आगे बढ़ाएं
current = next; // current आगे बढ़ाएं
}
return prev; // नया हेड
}
4. रिकर्सिव तरीका
public Node reverseRecursive(Node head) {
if (head == null || head.next == null) {
return head;
}
Node newHead = reverseRecursive(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
तुलना
| तरीका | समय | स्पेस | उपयोग |
|---|---|---|---|
| Collections.reverse() | O(n) | O(1) | प्रोडक्शन कोड |
| इटरेटिव (कस्टम Node) | O(n) | O(1) | इंटरव्यू, कस्टम लिस्ट |
| रिकर्सिव | O(n) | O(n) | शैक्षिक, छोटी लिस्ट |
मुख्य बिंदु
Collections.reverse()किसी भीListपर काम करता है- कस्टम लिंक्ड लिस्ट के लिए इटरेटिव तरीका बेस्ट है (O(1) स्पेस)
- रिकर्सिव तरीका स्टैक ओवरफ्लो का कारण बन सकता है
- Java के
LinkedListमेंdescendingIterator()भी है - मूल लिस्ट बदल जाती है (इन-प्लेस)