Related
I'm trying to solve the following problem:
Palindrome Linked List
Given the head of a singly linked list, return true if it is a
palindrome.
Input: head = [1,2,2,1]
Output: true
I'm getting a wrong result for the following test case:
[1,1,2,1]
Expected output is false, but my code returns true (screenshot)
My code:
class Solution {
public ListNode reverseRecursive(ListNode head)
{
if (head == null || head.next == null)
{
return head;
}
ListNode newHead = reverseRecursive(head.next);
head.next.next = head;
head.next = null;
return newHead;
}
public boolean isPalindrome(ListNode head) {
ListNode head1=reverseRecursive(head);
ListNode curr = head;
ListNode curr1 = head1;
while (curr != null && curr1 != null)
{
if(curr.val == curr1.val)
{
curr = curr.next;
curr1 = curr1.next;
} else {
return false;
}
}
return true;
}
}
I have used recursion to reverse the array and then compare the nodes of the linked list one by one, traversing through the list.
Class ListNode defined as follows:
public static class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
The logic inside isPalindrome() is correct. The issue resides in the reverseRecursive() method.
Firstly, you don't have to reverse the existing list. Secondly, this method actually doesn't reverse the list, the list gets truncated in such a way that it contains only two first nodes.
You don't need to alter the Linked List. Instead, you can generate a new Linked List, in which each node is a copy of the node from the initial list (i.e. it has the same value) and nodes are linked in reverse order. Then inside isPalindrome() we can assign the head of the newly created reversed list to the variable curr1.
Note that according to the constraints of this task it's guaranteed that there would be at least one node, hence head would never be null.
public boolean isPalindrome(ListNode head) {
ListNode revListHead = createReversedList(head);
ListNode curr = head;
ListNode curr1 = revListHead;
while (curr != null && curr1 != null) {
if (curr.val == curr1.val) {
curr = curr.next;
curr1 = curr1.next;
} else {
return false;
}
}
return true;
}
public ListNode createReversedList(ListNode head) {
ListNode prev;
ListNode currCopy = new ListNode(head.val);
ListNode curr = head;
while (curr.next != null) {
prev = currCopy;
curr = curr.next;
currCopy = new ListNode(curr.val, prev);
}
return currCopy;
}
main()
public static void main(String[] args) {
ListNode head = new ListNode(1);
ListNode cur = head;
int[] arr = {1, 2, 2, 1};
for (int i = 1; i < arr.length; i++) {
cur.next = new ListNode(arr[i]);
cur = cur.next;
}
System.out.println(isPalindrome(head));
cur = head;
arr = new int[]{1, 1, 2, 1};
for (int i = 1; i < arr.length; i++) {
cur.next = new ListNode(arr[i]);
cur = cur.next;
}
System.out.println(isPalindrome(head));
}
Output:
true // [1, 2, 2, 1]
false // [1, 1, 2, 1]
We can also approach this problem by dumping the values of all the nodes in a Linked List into an ArrayList.
And then iterate over its indices until the middle of the list (which is sufficient), checking the corresponding values at the front and at the back of the list.
public boolean isPalindrome(ListNode head) {
List<Integer> values = getListOfValues(head);
int size = values.size();
boolean isPalindrome = true;
for (int i = 0; i < size / 2; i++) {
if (!values.get(i).equals(values.get(size - 1 - i))) {
isPalindrome = false;
break;
}
}
return isPalindrome;
}
public List<Integer> getListOfValues(ListNode head) {
ListNode curr = head;
List<Integer> values = new ArrayList<>();
while (curr != null) {
values.add(curr.val);
curr = curr.next;
}
return values;
}
I have found the answer to this question using this algorithm that gives me the best time complexity that I can think of.
class Solution {
public ListNode reverse(ListNode head)
{
ListNode prev=null;
ListNode curr=head;
while(curr != null)
{
ListNode next=curr.next;
curr.next=prev;//Assigning
//Updation
prev=curr;
curr=next;
}
// head.next=null;
// head =prev;
return prev;
}
public ListNode findMiddle(ListNode head)
{
ListNode hare=head;
ListNode turtle=head;
while(hare.next !=null && hare.next.next !=null)
{
hare=hare.next.next;//Increment by two
turtle=turtle.next;//Increment by one
}
return turtle;//This will be at middle by then
}
public boolean isPalindrome(ListNode head) {
if(head==null || head.next==null)
{
return true;
}
ListNode middle= findMiddle(head);//1st half end
ListNode secondHalfStart=reverse(middle.next);
ListNode firstHalfStart=head;
while(secondHalfStart != null)
{
if(firstHalfStart.val==secondHalfStart.val)
{
firstHalfStart=firstHalfStart.next;
secondHalfStart=secondHalfStart.next;
}else{
return false;
}
}
return true;
}
}
In this, I have found the middle of the linked list using the hear and turtle approach(Floyd’s Algorithm).
I have reversed the second half of the list and compared the elements one by one.
reverse second half linkedlist
example:
even number:
2->1->3->4->5->6->7->8 =====> 2->1->3->4->8->7->6->5 ;
odd number: 5->7->8->6->3->4->2 ======> 5->7->8->2->4->3->6, the
middle one also need be reversed
class ListNode
{
int val;
ListNode next;
ListNode(int x) { val = x; }
}
class ReverseRightHalfLinkedList
{
public static void main(String[] args)
{
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
ListNode res = reverse(node1);//line 31
// ListNode node = node1;
// while (node != null)
// {
// System.out.println(node.val);
// node = node.next;
// }
}
public static ListNode reverse(ListNode start)
{
int counter = 0;
ListNode node = start;
ListNode pre = start;
while (node!= null)
{
counter += 1;
node = node.next;
}
for (int i=0; i<counter/2; i++)
{
pre = start;
start = start.next;
}
ListNode cur = start;
if (counter%2 ==0)
{
while (cur != null)
{
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
}
else
{
pre = pre.next;
cur = start.next;
System.out.println(pre.val);
System.out.println(cur.val);
while (cur != null)
{
ListNode temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
System.out.println("-----");
System.out.println(pre.val); // line 90
System.out.println(cur.val);
System.out.println("-----");
System.out.println();
}
}
return start;
}
}
Firstly, I got an error message.
Exception in thread "main" java.lang.NullPointerException at
ReverseRightHalfLinkedList.reverse(OA2.java:90) at
ReverseRightHalfLinkedList.main(OA2.java:31)
Secondly, I tried to print the order of reversed linked list, it is still in order. It was not reversed.
Please help me to figure out these two problems. Thanks a lot!
Basing on #passion's idea. I got a more concise code.
class ListNode
{
int val;
ListNode next;
ListNode(int x) { val = x; }
}
class ReverseRightHalfLinkedList
{
public static void main(String[] args)
{
ListNode node1 = new ListNode(2);
ListNode node2 = new ListNode(1);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
ListNode node6 = new ListNode(6);
ListNode node7 = new ListNode(7);
ListNode node8 = new ListNode(8);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
node6.next = node7;
node7.next = node8;
ListNode res = reverse(node1);
ListNode node = node1;
while (node != null)
{
System.out.println(node.val);
node = node.next;
}
}
public static ListNode reverse(ListNode start)
{
int counter = 0;
ListNode node = start;
ListNode pre = start;
ListNode result = start;
while (node!= null)// for count how many elements in linked list
{
counter += 1;
node = node.next;
}
for (int i=0; i< (counter / 2) ; i++)//no matter counter is even or odd, when it divided by 2, the result is even
{
pre = start;
start = start.next;
}
ListNode temp = null;
ListNode preNext = null;// this variable is used to track the next val behind pre
// for example, 2->1->3->4->5->6->7->8
// at this moment, pre:4, start:5
// I treated 5->6->7->8 as an independent linkedlist
// I reversed the linkedlist
// Finally, set the pre node's next value to the reversed linkedlist's head
// The first half and second half have been connected together
while (start != null)
{
temp = start.next;
start.next = preNext;
preNext = start;
start = temp;
}
pre.next = preNext;
return start;
}
}
You need reverse right half list , so you need to save last node of the left half and the header of the list , so than when right half get reversed , you can link them with the left half and return the whole list .
I have change your reverse method :
public static ListNode reverse(ListNode start)
{
int counter = 0;
ListNode node = start;
ListNode pre = start;
ListNode result = start;
while (node!= null)
{
counter += 1;
node = node.next;
}
int end = counter % 2 == 0 ? counter / 2 : (counter- 1) / 2 ;
for (int i=0; i< end ; i++)
{
pre = start;
start = start.next;
}
ListNode tlist = null,temp ;
while(start != null){
temp = start.next;
if(tlist == null){
tlist = start;
start.next = null;
}else{
start.next = tlist;
tlist = start;
}
start = temp;
}
pre.next = tlist;
return start;
}
Hope this code helps you understand.
class MainClass {
public static void main(String args[]) {
Node head = new Node("Sameer");
//Adding data to my linked list
Node temp = addNode("Monica", head);
temp = addNode("Doug", temp);
temp = addNode("Eric", temp);
temp = addNode("Charlie", temp);
temp = addNode("Dan", temp);
temp = addNode("Enrique", temp);
temp = addNode("Ankitha", temp);
addNode("Chad", temp);
SolveMidLinkedList(head);
}
//method to add a node to the linked list
static Node addNode(String str, Node node) {
Node newNode = new Node(str);
node.link = newNode;
return newNode;
}
//method to reverse the right hald of the linkedlist
static void SolveMidLinkedList(Node head) {
int LinkedListSize = 0;
Node temp = head;
Node LastEle = null, MiddleEle = null, temp1 = null, temp2 = null;
//While loop to find the size of the linkedlist and also to find the last element in the linkedlist
while (temp != null) {
LastEle = temp;
temp = temp.link;
LinkedListSize++;
}
//Printing the names
temp = head;
System.out.println("Before rearranging the linked list");
while (temp != null) {
System.out.println(temp.name);
temp = temp.link;
}
//The main procedure
temp = head;
int iCount = 1;
while (temp != null) {
//Not changing the order of first half of the linked list
if (iCount <= (LinkedListSize / 2)) {
MiddleEle = temp;
} else {
//Reversing the order of second half(right side half) of the linked list.
temp2 = temp.link;
temp.link = temp1;
temp1 = temp;
}
temp = temp.link;
iCount++;
}
//At the end asssigning the middle element to the last element of the linked list.
MiddleEle.link = LastEle;
//Printing the names
temp = head;
System.out.println("After rearranging the linked list");
while (temp != null) {
System.out.println(temp.name);
temp = temp.link;
}
}
}
//General definition of Node in a linked list.
class Node {
Node link = null;
String name;
Node(String str) {
this.name = str;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
class LinkedList {
static Node head;
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
Node rev(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;
}
Node reverse(Node node) {
int count =0;
Node doo = node;
Node mid_pre = node;
Node mid = node;
while(doo.next != null){
if((count & 1) == 1){
// mid_pre = mid;
mid = mid.next;
}
doo = doo.next;
count++;
}
// System.out.println(" midd ddddd :"+mid_pre.data+" "+mid.data);
mid.next = rev(mid.next);
return node;
}
void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
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);
//// 1 2 3 4 5 6
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);
}
}
Why not do something very straightforward. We know the size of the list. We can use list.get(counter) to iterate from the end to the centre. There was a bit of challenge when the list size was even or odd but its doable.
public static void reverseFromEndToCentre() {
List list = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));
int midOfList = (int)Math.ceil(list.size() / 2);
int lenthOfList = list.size();
int counterFirstHalf =0;
while(counterFirstHalf<midOfList){
System.out.println(list.get(counterFirstHalf));
counterFirstHalf++;
}
int counterSecondHalf = lenthOfList;
while( counterSecondHalf > counterFirstHalf){
counterSecondHalf--;
System.out.println(list.get(counterSecondHalf ));
}
}
Here's a way to reverse second half(right) of linked list.
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node* next;
};
void append(struct Node** head_ref, int new_data)
{
struct Node* new_node=(struct Node*)malloc(sizeof(struct Node));
new_node->data=new_data;
new_node->next=NULL;
struct Node* last=*head_ref;
if(*head_ref==NULL){
*head_ref=new_node;
return;
}
while(last->next!=NULL)
last=last->next;
last->next=new_node;
return;
}
int display(struct Node* n)
{
int m=0;
printf("\n");
while(n!=NULL)
{
printf(" %d ",n->data);
n=n->next;
m=m+1;
}
return m;
}
void reverse(struct Node** head_ref, int mid)
{
if(*head_ref==NULL)
{
printf("\nEmpty List cannot be reversed");
return;
}
struct Node* last=*head_ref;
struct Node* second_last;
struct Node* beg=*head_ref;
int c=1;
while(c<=mid){
second_last=last;
last=last->next;
c=c+1;
}
struct Node* prev=NULL;
struct Node* current=last;
struct Node* next;
while(current != NULL)
{
next=current->next;
current->next=prev;
prev=current;
current=next;
}
*head_ref=beg;
second_last->next=prev;
}
int main()
{
int size;
struct Node* head=NULL;
int i,mid;
for(i=0;i<11;i++)
{
append(&head,rand()%19 +1);
}
size=display(head);
printf("\n Size of linked list: %d",size);
if(size%2==0)
mid=(size+1)/2;
else
mid=size/2;
reverse(&head, mid);
display(head);
return 0;
}
I recently encounter an algorithm problem:
Reverse a singly-linked list in blocks of k in place. An iterative approach is preferred.
The first block of the resulting list should be maximal with regards to k. If the list contains n elements, the last block will either be full or contain n mod k elements.
For example:
k = 2, list = [1,2,3,4,5,6,7,8,9], the reversed list is [8,9,6,7,4,5,2,3,1]
k = 3, list = [1,2,3,4,5,6,7,8,9], the reversed list is [7,8,9,4,5,6,1,2,3]
My code is shown as below.
Is there an O(n) algorithm that doesn't use a stack or extra space?
public static ListNode reverse(ListNode list, int k) {
Stack<ListNode> stack = new Stack<ListNode>();
int listLen = getLen(list);
int firstBlockSize = listLen % k;
ListNode start = list;
if (firstBlockSize != 0) {
start = getBlock(stack, start, firstBlockSize);
}
int numBlock = (listLen) / k;
for (int i = 0; i < numBlock; i++) {
start = getBlock(stack, start, k);
}
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
while (!stack.empty()) {
cur.next = stack.peek();
cur = stack.pop();
while (cur.next != null) {
cur = cur.next;
}
}
return dummy.next;
}
public static ListNode getBlock(Stack<ListNode> stack, ListNode start, int blockSize) {
ListNode end = start;
while (blockSize > 1) {
end = end.next;
blockSize--;
}
ListNode temp = end.next;
end.next = null;
stack.push(start);
return temp;
}
public static int getLen(ListNode list) {
ListNode iter = list;
int len = 0;
while (iter != null) {
len++;
iter = iter.next;
}
return len;
}
This can be done in O(n) time using O(1) space as follows:
Reverse the entire list.
Reverse the individual blocks.
Both can be done using something very similar to the standard way to reverse a singly linked-list and the overall process resembles reversing the ordering of words in a string.
Reversing only a given block is fairly easily done by using a length variable.
The complication comes in when we need to move from one block to the next. The way I achieved this was by having the reverse function return both the first and last nodes and having the last node point to the next node in our original linked-list. This is necessary because the last node's next pointer needs to be updated to point to the first node our next reverse call returns (we don't know what it will need to point to before that call completes).
For simplicity's sake, I used a null start node in the code below (otherwise I would've needed to cater for the start node specifically, which would've complicated the code).
class Test
{
static class Node<T>
{
Node next;
T data;
Node(T data) { this.data = data; }
#Override
public String toString() { return data.toString(); }
}
// reverses a linked-list starting at 'start', ending at min(end, len-th element)
// returns {first element, last element} with (last element).next = (len+1)-th element
static <T> Node<T>[] reverse(Node<T> start, int len)
{
Node<T> current = start;
Node<T> prev = null;
while (current != null && len > 0)
{
Node<T> temp = current.next;
current.next = prev;
prev = current;
current = temp;
len--;
}
start.next = current;
return new Node[]{prev, start};
}
static <T> void reverseByBlock(Node<T> start, int k)
{
// reverse the complete list
start.next = reverse(start.next, Integer.MAX_VALUE)[0];
// reverse the individual blocks
Node<T>[] output;
Node<T> current = start;
while (current.next != null)
{
output = reverse(current.next, k);
current.next = output[0];
current = output[1];
}
}
public static void main(String[] args)
{
//Scanner scanner = new Scanner(System.in);
Scanner scanner = new Scanner("3 9 1 2 3 4 5 6 7 8 9\n" +
"2 9 1 2 3 4 5 6 7 8 9");
while (scanner.hasNextInt())
{
int k = scanner.nextInt();
// read the linked-list from console
Node<Integer> start = new Node<>(null);
Node<Integer> current = start;
int n = scanner.nextInt();
System.out.print("Input: ");
for (int i = 1; i <= n; i++)
{
current.next = new Node<>(scanner.nextInt());
current = current.next;
System.out.print(current.data + " ");
}
System.out.println("| k = " + k);
// reverse the list
reverseByBlock(start, k);
// display the list
System.out.print("Result: ");
for (Node<Integer> node = start.next; node != null; node = node.next)
System.out.print(node + " ");
System.out.println();
System.out.println();
}
}
}
This outputs:
Input: 1 2 3 4 5 6 7 8 9 | k = 3
Result: 7 8 9 4 5 6 1 2 3
Input: 1 2 3 4 5 6 7 8 9 | k = 2
Result: 8 9 6 7 4 5 2 3 1
Live demo.
Here is an iterative way of doing it... you read my full explanation here
Node reverseListBlocks1(Node head, int k) {
if (head == null || k <= 1) {
return head;
}
Node newHead = head;
boolean foundNewHead = false;
// moves k nodes through list each loop iteration
Node mover = head;
// used for reversion list block
Node prev = null;
Node curr = head;
Node next = null;
Finish: while (curr != null) {
// move the mover just after the block we are reversing
for (int i = 0; i < k; i++) {
// if there are no more reversals, finish
if (mover == null) {
break Finish;
}
mover = mover.next;
}
// reverse the block and connect its tail to the rest of
// the list (at mover's position)
prev = mover;
while (curr != mover) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
// establish the new head, if we didn't yet
if (!foundNewHead) {
newHead = prev;
foundNewHead = true;
}
// connects previous block's head to the rest of the list
// move the head to the tail of the reversed block
else {
head.next = prev;
for (int i = 0; i < k; i++) {
head = head.next;
}
}
}
return newHead;
}
The easiest way to reverse the single linked list is as follows.
private void reverse(Node node)
{
Node current = Node;
Node prev = null;
Node next = null;
if(node == null || node.next == null)
{
return node;
}
while(current != null)
{
next = current.next;
//swap
current.next = prev;
prev = current;
current = next;
}
node.next = current;
}
This section contains multiple Single Linked List Operation
import java.util.Scanner;
import java.util.Stack;
public class AddDigitPlusLL {
Node head = null;
int len =0;
static class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public void insertFirst(int data) {
Node newNode = new Node(data);
if (head != null)
newNode.next = head;
head = newNode;
}
public void insertLast(int data) {
Node temp = head;
Node newNode = new Node(data);
if (temp == null)
head = newNode;
else {
Node previousNode = null;
while (temp != null) {
previousNode = temp;
temp = temp.next;
}
previousNode.next = newNode;
}
}
public Long getAllElementValue() {
Long val = 0l;
Node temp=head;
while(temp !=null) {
if(val == 0)
val=(long) temp.data;
else
val=val*10+temp.data;
temp = temp.next;
}
System.out.println("value is :" + val);
return val;
}
public void print(String printType) {
System.out.println("----------- :"+ printType +"----------- ");
Node temp = head;
while (temp != null) {
System.out.println(temp.data + " --> ");
temp = temp.next;
}
}
public void generateList(Long val) {
head = null;
while(val > 0) {
int remaining = (int) (val % 10);
val = val/10;
insertFirst(remaining);
}
}
public void reverseList(Long val) {
head =null;
while(val >0) {
int remaining = (int) (val % 10);
val = val/10;
insertLast(remaining);
}
}
public void lengthRecursive(Node temp) {
if(temp != null) {
len++;
lengthRecursive(temp.next);
}
}
public void reverseUsingStack(Node temp) {
Stack<Integer> stack = new Stack<Integer>();
while(temp != null) {
stack.push(temp.data);
temp = temp.next;
}
head = null;
while(!stack.isEmpty()) {
int val = stack.peek();
insertLast(val);
stack.pop();
}
print(" Reverse Using Stack");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s = new Scanner(System.in);
AddDigitPlusLL sll = new AddDigitPlusLL();
sll.insertFirst(5);
sll.insertFirst(9);
sll.insertLast(8);
sll.print("List Iterate");
Long val = sll.getAllElementValue();
System.out.println("Enter the digit to add");
Long finalVal = val +s.nextInt();
s.close();
sll.generateList(finalVal);
sll.print("Add int with List Value");
sll.reverseList(finalVal);
sll.print("Reverse the List");
sll.lengthRecursive(sll.head);
System.out.println("Length with Recursive :"+ sll.len);
sll.print("Before call stack reverse method");
sll.reverseUsingStack(sll.head);
}
}
I have implemented a java code that reverses every k Node in the linked list and here is the code:
public class ReverseALinkedList {
public static void main(String[] args) {
int k = 2;
Node a = new Node(1);
Node b = new Node(2);
Node c = new Node(3);
Node d = new Node(4);
Node e = new Node(5);
Node f = new Node(6);
Node g = new Node(7);
Node h = new Node(8);
a.next = b;
b.next = c;
c.next = d;
d.next = e;
e.next = f;
f.next = g;
g.next = h;
a.printLinkedList();
Node head = reverseKLinkedList(a, k);
head.printLinkedList();
}
private static Node reverseKLinkedList(Node first, int k) {
Node current;
Node temp;
int count = 0;
current = null;
while (first != null && count < k) {
temp = first.next;
first.next = current;
current = first;
first = temp;
++count;
}
if (first != null) {
first.next = reverseKLinkedList(first, k);
}
return current;
}
static class Node {
public Node next;
public int value;
public Node(int value) {
this.value = value;
}
public void printLinkedList() {
Node head = this;
while (head.next != null) {
System.out.print(head.value + "->");
head = head.next;
}
System.out.print(head.value + "->null");
System.out.println();
}
}
}
When I execute the code with the following linked list:
1->2->3->4->5->6->null and k set to 2, I get an output as follows:
2->1->null
The rest of the nodes gets reversed (i.e., 4->3, 6->5), but they are not returned during the recursive call.
Could anybody please let me know how to fix this?
Another way of doing it is to ITERATE every K nodes and store it in a S stack like manner.
Wherein every S is stored in N newList in every K iteration.
private static Node reverseKLinkedList(Node first, int k){
Node newList, temp, current, walk, node;
int count;
node = first;
newList = null;
while(node != null) {
count = 0;
// stack the nodes to current until node is null or count is less than k
current = null;
while(node != null && count < k) {
temp = current;
current = new Node(node.value);
current.next = temp;
node = node.next;
count++;
}
if(newList == null) // if newList is empty then assign the current node
newList = current;
else {
// else traverse against the newList until it reaches
// the last node and then append the current not
walk = newList;
while(walk.next != null) walk = walk.next;
walk.next = current;
}
}
return newList;
}
As you are reversing the first one you are losing the reference to the next Node and therefore you lose the rest of the elements.
To implement this you should use recursion in order to store nodes or a stack(which is basically the same as recursion).
Proceed like this:
Store all the Nodes from the list in a stack.
For each node poped, the next elem is the top of the stack.
Repeat until no more nodes left.
Here is the recursive Java implementation
public static <T> LinearNode<T> reverseAlternateKNodes(LinearNode<T> node, int k) {
int count = 0;
LinearNode<T> curr=node, prev = null, next = null;
//reverse k nodes
while (curr != null && count < k) {
next = curr.next();
curr.next(prev);
prev = curr;
curr = next;
count ++;
}
// head should point to start of next k node
node.next(curr);
// skip nex knodes
count = 0;
while (curr != null && count < k- 1) {
curr = curr.next();
count ++;
}
// do it recursively
if (curr != null) {
curr.next(reverseAlternateKNodes(curr.next(), k));
}
return prev;
}
Here is the unit tests
#Test
public void reverseAlternateKNodes() {
LinearNode<Integer> head = buildLinkedList(1,2,3,4,5,6,7,8,9,10);
head = LinkedListUtil.reverseAlternateKNodes(head, 3);
assertLinkedList(head, 3,2,1,4,5,6,9,8,7,10);
}
public ListNode reverseKGroup(ListNode head, int k) {
ListNode curr = head;
int count = 0;
while(curr!=null && count!=k){
count++;
curr = curr.next;
}
if(count==k){
curr = reverseKGroup(curr,k);
while(count-->0){
ListNode temp = head.next;
head.next = curr;
curr = head;
head = temp;
}
head = curr;
}
return head;
}
This question already has answers here:
How to reverse a singly-linked list in blocks of some given size in O(n) time in place?
(4 answers)
Closed 4 years ago.
Can someone tell me why my code dosent work? I want to reverse a single linked list in java: This is the method (that doesnt work correctly)
public void reverseList(){
Node before = null;
Node tmp = head;
Node next = tmp.next;
while(tmp != null){
if(next == null)
return;
tmp.next = before;
before = tmp;
tmp = next;
next = next.next;
}
}
And this is the Node class:
public class Node{
public int data;
public Node next;
public Node(int data, Node next){
this.data = data;
this.next = next;
}
}
On input 4->3->2->1 I got output 4. I debugged it and it sets pointers correctly but still I dont get why it outputs only 4.
Node next = tmp.next;
while(tmp != null){
So what happens when tmp == null?
You almost got it, though.
Node before = null;
Node tmp = head;
while (tmp != null) {
Node next = tmp.next;
tmp.next = before;
before = tmp;
tmp = next;
}
head = before;
Or in nicer (?) naming:
Node reversedPart = null;
Node current = head;
while (current != null) {
Node next = current.next;
current.next = reversedPart;
reversedPart = current;
current = next;
}
head = reversedPart;
ASCII art:
<__<__<__ __ : reversedPart : head
(__)__ __ __
head : current: > > >
public Node<E> reverseList(Node<E> node) {
if (node == null || node.next == null) {
return node;
}
Node<E> currentNode = node;
Node<E> previousNode = null;
Node<E> nextNode = null;
while (currentNode != null) {
nextNode = currentNode.next;
currentNode.next = previousNode;
previousNode = currentNode;
currentNode = nextNode;
}
return previousNode;
}
The method for reversing a linked list is as below;
Reverse Method
public void reverseList() {
Node<E> curr = head;
Node<E> pre = null;
Node<E> incoming = null;
while(curr != null) {
incoming = curr.next; // store incoming item
curr.next = pre; // swap nodes
pre = curr; // increment also pre
curr = incoming; // increment current
}
head = pre; // pre is the latest item where
// curr is null
}
Three references are needed to reverse a list: pre, curr, incoming
... pre curr incoming
... --> (n-1) --> (n) --> (n+1) --> ...
To reverse a node, you have to store previous element, so that you can use the simple stament;
curr.next = pre;
To reverse the current element's direction. However, to iterate over the list, you have to store incoming element before the execution of the statement above because as reversing the current element's next reference, you don't know the incoming element anymore, that's why a third reference needed.
The demo code is as below;
LinkedList Sample Class
public class LinkedList<E> {
protected Node<E> head;
public LinkedList() {
head = null;
}
public LinkedList(E[] list) {
this();
addAll(list);
}
public void addAll(E[] list) {
for(int i = 0; i < list.length; i++)
add(list[i]);
}
public void add(E e) {
if(head == null)
head = new Node<E>(e);
else {
Node<E> temp = head;
while(temp.next != null)
temp = temp.next;
temp.next = new Node<E>(e);
}
}
public void reverseList() {
Node<E> curr = head;
Node<E> pre = null;
Node<E> incoming = null;
while(curr != null) {
incoming = curr.next; // store incoming item
curr.next = pre; // swap nodes
pre = curr; // increment also pre
curr = incoming; // increment current
}
head = pre; // pre is the latest item where
// curr is null
}
public void printList() {
Node<E> temp = head;
System.out.print("List: ");
while(temp != null) {
System.out.print(temp + " ");
temp = temp.next;
}
System.out.println();
}
public static class Node<E> {
protected E e;
protected Node<E> next;
public Node(E e) {
this.e = e;
this.next = null;
}
#Override
public String toString() {
return e.toString();
}
}
}
Test Code
public class ReverseLinkedList {
public static void main(String[] args) {
Integer[] list = { 4, 3, 2, 1 };
LinkedList<Integer> linkedList = new LinkedList<Integer>(list);
linkedList.printList();
linkedList.reverseList();
linkedList.printList();
}
}
Output
List: 4 3 2 1
List: 1 2 3 4
If this isn't homework and you are doing this "manually" on purpose, then I would recommend using
Collections.reverse(list);
Collections.reverse() returns void, and your list is reversed after the call.
We can have three nodes previous,current and next.
public void reverseLinkedlist()
{
/*
* Have three nodes i.e previousNode,currentNode and nextNode
When currentNode is starting node, then previousNode will be null
Assign currentNode.next to previousNode to reverse the link.
In each iteration move currentNode and previousNode by 1 node.
*/
Node previousNode = null;
Node currentNode = head;
while (currentNode != null)
{
Node nextNode = currentNode.next;
currentNode.next = previousNode;
previousNode = currentNode;
currentNode = nextNode;
}
head = previousNode;
}
public void reverse() {
Node prev = null; Node current = head; Node next = current.next;
while(current.next != null) {
current.next = prev;
prev = current;
current = next;
next = current.next;
}
current.next = prev;
head = current;
}
// 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;
}
}
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);
}
}
OUTPUT: -
Given Linked list
85 15 4 20
Reversed linked list
20 4 15 85
I know the recursive solution is not the optimal one, but just wanted to add one here:
public class LinkedListDemo {
static class Node {
int val;
Node next;
public Node(int val, Node next) {
this.val = val;
this.next = next;
}
#Override
public String toString() {
return "" + val;
}
}
public static void main(String[] args) {
Node n = new Node(1, new Node(2, new Node(3, new Node(20, null))));
display(n);
n = reverse(n);
display(n);
}
static Node reverse(Node n) {
Node tail = n;
while (tail.next != null) {
tail = tail.next;
}
reverseHelper(n);
return (tail);
}
static Node reverseHelper(Node n) {
if (n.next != null) {
Node reverse = reverseHelper(n.next);
reverse.next = n;
n.next = null;
return (n);
}
return (n);
}
static void display(Node n) {
for (; n != null; n = n.next) {
System.out.println(n);
}
}
}
I don't get it... why not doing this :
private LinkedList reverseLinkedList(LinkedList originalList){
LinkedList reversedList = new LinkedList<>();
for(int i=0 ; i<originalList.size() ; i++){
reversedList.add(0, originalList.get(i));
}
return reversedList;
}
I find this easier.
A more elegant solution would be to use recursion
void ReverseList(ListNode current, ListNode previous) {
if(current.Next != null)
{
ReverseList(current.Next, current);
ListNode temp = current.Next;
temp.Next = current;
current.Next = previous;
}
}
I tried the below code and it works fine:
Node head = firstNode;
Node current = head;
while(current != null && current.next != null){
Node temp = current.next;
current.next = temp.next;
temp.next = head;
head = temp;
}
Basically one by one it sets the next pointer of one node to its next to next node, so from next onwards all nodes are attached at the back of the list.
Node reverse_rec(Node start) {
if (start == null || start -> next == null) {
return start;
}
Node new_start = reverse(start->next);
start->next->next = start;
start->next = null;
return new_start;
}
Node reverse(Node start) {
Node cur = start;
Node bef = null;
while (cur != null) {
Node nex = cur.next;
cur.next = bef;
bef = cur;
cur = nex;
}
return bef;
}
I think your problem is that your initially last element next attribute isn't being changed becuase of your condition
if(next == null)
return;
Is at the beginning of your loop.
I would move it right after tmp.next has been assigned:
while(tmp != null){
tmp.next = before;
if(next == null)
return;
before = tmp;
tmp = next;
next = next.next;
}
Use this.
if (current== null || current.next==null) return current;
Node nextItem = current.next;
current.next = null;
Node reverseRest = reverse(nextItem);
nextItem.next = current;
return reverseRest
or Java Program to reverse a Singly Linked List
package com.three;
public class Link {
int a;
Link Next;
public Link(int i){
a=i;
}
}
public class LinkList {
Link First = null;
public void insertFirst(int a){
Link objLink = new Link(a);
objLink.Next=First;
First = objLink;
}
public void displayLink(){
Link current = First;
while(current!=null){
System.out.println(current.a);
current = current.Next;
}
}
public void ReverseLink(){
Link current = First;
Link Previous = null;
Link temp = null;
while(current!=null){
if(current==First)
temp = current.Next;
else
temp=current.Next;
if(temp==null){
First = current;
//return;
}
current.Next=Previous;
Previous=current;
//System.out.println(Previous);
current = temp;
}
}
public static void main(String args[]){
LinkList objLinkList = new LinkList();
objLinkList.insertFirst(1);
objLinkList.insertFirst(2);
objLinkList.insertFirst(3);
objLinkList.insertFirst(4);
objLinkList.insertFirst(5);
objLinkList.insertFirst(6);
objLinkList.insertFirst(7);
objLinkList.insertFirst(8);
objLinkList.displayLink();
System.out.println("-----------------------------");
objLinkList.ReverseLink();
objLinkList.displayLink();
}
}
You can also try this
LinkedListNode pointer = head;
LinkedListNode prev = null, curr = null;
/* Pointer variable loops through the LL */
while(pointer != null)
{
/* Proceed the pointer variable. Before that, store the current pointer. */
curr = pointer; //
pointer = pointer.next;
/* Reverse the link */
curr.next = prev;
/* Current becomes previous for the next iteration */
prev = curr;
}
System.out.println(prev.printForward());
package LinkedList;
import java.util.LinkedList;
public class LinkedListNode {
private int value;
private LinkedListNode next = null;
public LinkedListNode(int i) {
this.value = i;
}
public LinkedListNode addNode(int i) {
this.next = new LinkedListNode(i);
return next;
}
public LinkedListNode getNext() {
return next;
}
#Override
public String toString() {
String restElement = value+"->";
LinkedListNode newNext = getNext();
while(newNext != null)
{restElement = restElement + newNext.value + "->";
newNext = newNext.getNext();}
restElement = restElement +newNext;
return restElement;
}
public static void main(String[] args) {
LinkedListNode headnode = new LinkedListNode(1);
headnode.addNode(2).addNode(3).addNode(4).addNode(5).addNode(6);
System.out.println(headnode);
headnode = reverse(null,headnode,headnode.getNext());
System.out.println(headnode);
}
private static LinkedListNode reverse(LinkedListNode prev, LinkedListNode current, LinkedListNode next) {
current.setNext(prev);
if(next == null)
return current;
return reverse(current,next,next.getNext());
}
private void setNext(LinkedListNode prev) {
this.next = prev;
}
}
public class ReverseLinkedList {
public static void main(String args[]){
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("a");
linkedList.add("b");
linkedList.add("c");
linkedList.add("d");
linkedList.add("e");
linkedList.add("f");
System.out.println("Original linkedList:");
for(int i = 0; i <=linkedList.size()-1; i++){
System.out.println(" - "+ linkedList.get(i));
}
LinkedList<String> reversedlinkedList = reverse(linkedList);
System.out.println("Reversed linkedList:");
for(int i = 0; i <=reversedlinkedList.size()-1; i++){
System.out.println(" - "+ reversedlinkedList.get(i));
}
}
public static LinkedList<String> reverse(LinkedList<String> linkedList){
for(int i = 0; i < linkedList.size()/2; i++){
String temp = linkedList.get(i);
linkedList.set(i, linkedList.get(linkedList.size()-1-i));
linkedList.set((linkedList.size()-1-i), temp);
}
return linkedList;
}
}
To reverse a singly linked list you should have three nodes, top, beforeTop and AfterTop. Top is the header of singly linked list, hence beforeTop would be null and afterTop would be next element of top and with each iteration move forward beforeTop is assigned top and top is assigned afterTop(i.e. top.next).
private static Node inverse(Node top) {
Node beforeTop=null, afterTop;
while(top!=null){
afterTop=top.next;
top.next=beforeTop;
beforeTop=top;
top=afterTop;
}
return beforeTop;
}
Using Recursion It's too easy :
package com.config;
import java.util.Scanner;
public class Help {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
Node head = null;
Node temp = null;
int choice = 0;
boolean flage = true;
do{
Node node = new Node();
System.out.println("Enter Node");
node.data = sc.nextInt();
if(flage){
head = node;
flage = false;
}
if(temp!=null)
temp.next = node;
temp = node;
System.out.println("Enter 0 to exit.");
choice = sc.nextInt();
}while(choice!=0);
Help.getAll(head);
Node reverse = Help.reverse(head,null);
//reverse = Help.reverse(head, null);
Help.getAll(reverse);
}
public static void getAll(Node head){
if(head==null)
return ;
System.out.println(head.data+"Memory Add "+head.hashCode());
getAll(head.next);
}
public static Node reverse(Node head,Node tail){
Node next = head.next;
head.next = tail;
return (next!=null? reverse(next,head) : head);
}
}
class Node{
int data = 0;
Node next = null;
}
Node Reverse(Node head) {
Node n,rev;
rev = new Node();
rev.data = head.data;
rev.next = null;
while(head.next != null){
n = new Node();
head = head.next;
n.data = head.data;
n.next = rev;
rev = n;
n=null;
}
return rev;
}
Use above function to reverse single linked list.
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
check more details about complexity analysis
http://javamicro.com/ref-card/DS-Algo/How-to-Reverse-Singly-Linked-List?
public static LinkedList reverseLinkedList(LinkedList node) {
if (node == null || node.getNext() == null) {
return node;
}
LinkedList remaining = reverseLinkedList(node.getNext());
node.getNext().setNext(node);
node.setNext(null);
return remaining;
}
/**
* Reverse LinkedList
* #author asharda
*
*/
class Node
{
int data;
Node next;
Node(int data)
{
this.data=data;
}
}
public class ReverseLinkedList {
static Node root;
Node temp=null;
public void insert(int data)
{
if(root==null)
{
root=new Node(data);
}
else
{
temp=root;
while(temp.next!=null)
{
temp=temp.next;
}
Node newNode=new Node(data);
temp.next=newNode;
}
}//end of insert
public void display(Node head)
{
while(head!=null)
{
System.out.println(head.data);
head=head.next;
}
}
public Node reverseLinkedList(Node head)
{
Node newNode;
Node tempr=null;
while(head!=null)
{
newNode=new Node(head.data);
newNode.next=tempr;
tempr=newNode;
head=head.next;
}
return tempr;
}
public static void main(String[] args) {
ReverseLinkedList r=new ReverseLinkedList();
r.insert(10);
r.insert(20);
r.insert(30);
r.display(root);
Node t=r.reverseLinkedList(root);
r.display(t);
}
}
public class SinglyLinkedListImpl<T> {
private Node<T> head;
public void add(T element) {
Node<T> item = new Node<T>(element);
if (head == null) {
head = item;
} else {
Node<T> temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = item;
}
}
private void reverse() {
Node<T> temp = null;
Node<T> next = null;
while (head != null) {
next = head.next;
head.next = temp;
temp = head;
head = next;
}
head = temp;
}
void printList(Node<T> node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static void main(String a[]) {
SinglyLinkedListImpl<Integer> sl = new SinglyLinkedListImpl<Integer>();
sl.add(1);
sl.add(2);
sl.add(3);
sl.add(4);
sl.printList(sl.head);
sl.reverse();
sl.printList(sl.head);
}
static class Node<T> {
private T data;
private Node<T> next;
public Node(T data) {
super();
this.data = data;
}
}
}
public class Linkedtest {
public static void reverse(List<Object> list) {
int lenght = list.size();
for (int i = 0; i < lenght / 2; i++) {
Object as = list.get(i);
list.set(i, list.get(lenght - 1 - i));
list.set(lenght - 1 - i, as);
}
}
public static void main(String[] args) {
LinkedList<Object> st = new LinkedList<Object>();
st.add(1);
st.add(2);
st.add(3);
st.add(4);
st.add(5);
Linkedtest.reverse(st);
System.out.println("Reverse Value will be:"+st);
}
}
This will be useful for any type of collection Object.