My problem occurs when I track lastC, the last node in my linked list. For some reason, outside of the method it seems to be null constantly. I want to see if there is a better way to track it to improve run time on my program.
Append:
public MyStringBuilder append(String s)
{
if(this.length == 0) {
firstC = new CNode(s.charAt(0));
length = 1;
CNode currNode = firstC;
// Create remaining nodes, copying from String. Note
// how each new node is simply added to the end of the
// previous one. Trace this to see what is going on.
for (int i = 1; i < s.length(); i++)
{
CNode newNode = new CNode(s.charAt(i));
currNode.next = newNode;
currNode = newNode;
length++;
}
lastC = currNode;
//System.out.println(lastC.data);
//lastC.next = null;//new
} else {
if(lastC == null) {
lastC = getCNodeAt(length - 1);
//System.out.println("no");
}
//System.out.println(lastC.data);
//CNode currNode = lastC;
//CNode currNode = firstC;//changed
//for(int j = 0; j < length - 1; j++) {
// currNode = currNode.next;
//}
CNode currNode = lastC;
//lastC = currNode;
for (int i = 0; i < s.length(); i++)
{
CNode newNode = new CNode(s.charAt(i));
currNode.next = newNode;
currNode = newNode;//
length++;
}
lastC = currNode;
}
//lastC = getCNodeAt(length - 1);
return this;
}
Constructor:
public MyStringBuilder(String s)
{
if (s == null || s.length() == 0) // Special case for empty String
{ // or null reference
firstC = null;
lastC = null;
length = 0;
}
else
{
// Create front node to get started
firstC = new CNode(s.charAt(0));
length = 1;
CNode currNode = firstC;
// Create remaining nodes, copying from String. Note
// how each new node is simply added to the end of the
// previous one. Trace this to see what is going on.
for (int i = 1; i < s.length(); i++)
{
CNode newNode = new CNode(s.charAt(i));
currNode.next = newNode;
currNode = newNode;
length++;
}
lastC = currNode;
//lastC = getCNodeAt(length - 1);
//System.out.println(lastC.data);
}
}
Instance variables
private CNode firstC; // reference to front of list. This reference is necessary
// to keep track of the list
private CNode lastC; // reference to last node of list. This reference is
// necessary to improve the efficiency of the append()
// method
private int length;
Suggestions:
use sentinel node to get rid of unnecessary ifs
conventionally, append() should be void
constructor calls append() since they are doing similar jobs
Not a Java programmer, but something like this should work:
public class MyStringBuilder {
private CNode sentinel = new CNode('s');
private CNode lastC = sentinel;
private int length = 0;
public MyStringBuilder(String s){
append(s);
}
public void append(String s){
for (int i = 1; i < s.length(); i++){
CNode newNode = new CNode(s.charAt(i));
lastC.next = newNode;
lastC = newNode;
length++;
}
}
public CNode getHead(){
return sentinel.next;
}
public CNode getTail(){
if (length) {
return lastC;
} else {
return null;
}
}
}
Related
In this linked list I am trying to delete a node and return the list after deleting the particular node. Here I am not using the Value to delete, I am using the position to delete that particular node. Here in my code the delete function seems to have no effect over the output. What am I doing wrong here?
import java.util.*;
class LinkedList{
Node head;
static class Node{
int data;
Node next;
Node(int d){
this.data = d;
next = null;
}
}
public LinkedList insert(LinkedList l, int data){
Node new_node = new Node(data);
if(l.head == null){
l.head = new_node;
}
else{
Node last = l.head;
while(last.next != null){
last = last.next;
}
last.next = new_node;
}
return l;
}
public LinkedList delete(LinkedList l, int position){
Node current = l.head;
if(position == 0){
current = current.next;
}
int index = 1;
while(index < position - 1){
current = current.next;
index++;
}
current = current.next.next;
Node iterating = l.head;
while(iterating != null){
System.out.print(iterating.data + " ");
iterating = iterating.next;
}
return l;
}
}
public class Main
{
public static void main(String[] args) {
LinkedList l = new LinkedList();
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
int position = sc.nextInt();
for(int i=0; i<number; i++){
int num = sc.nextInt();
l.insert(l,num);
}
l.delete(l,position);
}
}
current=current.next doesn't have any effect on the original LinkedList because with that line of code, you just change where the reference (named as current) points to.
Think about this way,
Node a = new Node()
Node b = new Node()
Node c = new Node()
a.next =b
a = c
These lines of code doesn't result in c being connected to b.
Change code to this:
if (position == 0) return l.head.next;
else {
Node head = l.head;
int index = 1;
Node itr = l.head;
while(index < position) {
itr = itr.next;
index++;
}
if(itr.next != null) itr.next = itr.next.next;
return head;
}
public LinkedList delete(LinkedList l, int position) {
Node previous = null;
Node current = l.head;
int index = 0;
while (current != null && index < position){
previous = current;
current = current.next;
index++;
}
if (current != null) {
if (previous == null) {
l.head = current.next;
} else {
previous.next = current.next;
}
}
System.out.print("[");
Node iterating = l.head;
while (iterating != null) {
System.out.print(iterating.data + ", ");
iterating = iterating.next;
}
System.out.println("]");
return l;
}
The problem in java is that to delete a node either the head must be changed to its next, or the previous node's next must be changed to current's next.
Then too current might become null, have reached the list's end, position > list length.
public class SimpleLinkedList<E> {
public Node<E> head;
public int size;
public void add(E e) {
++this.size;
if (null == head) {
this.head = new Node();
head.val = e;
} else {
Node<E> newNode = new Node();
newNode.val = e;
newNode.next = head;
this.head = newNode;
}
}
public void swap(E val1, E val2) {
if (val1.equals(val2)) {
return;
}
Node prevX = null, curr1 = head;
while (curr1 != null && !curr1.val.equals(val1)) {
prevX = curr1;
curr1 = curr1.next;
}
Node prevY = null, curr2 = head;
while (curr2 != null && !curr2.val.equals(val2)) {
prevY = curr2;
curr2 = curr2.next;
}
if (curr1 == null || curr2 == null) {
return;
}
if (prevX == null) {
head = curr2;
} else {
prevX.next = curr2;
}
if (prevY == null) {
head = curr1;
} else {
prevY.next = curr1;
}
Node temp = curr1.next;
curr1.next = curr2.next;
curr2.next = temp;
}
public void reverse() {
Node<E> prev = null;
Node<E> current = head;
Node<E> next = null;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
head = prev;
}
public static class Node<E> {
public Node<E> next;
public E val;
}
}
public class SimpleLinkedListTest {
#Test
public void testReverseMethod() {
SimpleLinkedList<Integer> myList = new SimpleLinkedList<>();
for (int i = 0; i < 10; i++) {
myList.add(i);
}
SimpleLinkedList<Integer> expectedList = new SimpleLinkedList<>();
for (int i = 9; i > -1; i--) {
expectedList.add(i);
}
myList.reverse();
assertTrue(AssertCustom.assertSLLEquals(expectedList, myList));
}
}
What would be the most optimal way to reverse generic LinkedList by using the swap method?
before reverse method :
(head=[9])->[8]->[7]->[6]->[5]->[4]->[3]->[2]->[1]->[0]-> null
after reverse() method :
(head=[0])->[1]->[2]->[3]->[4]->[5]->[6]->[7]->[8]->[9]-> null
What you need to do is divide the list in half. If the list size is odd the one in the middle will remain in place. Then swap elements on either side in a mirror like fashion. This should be more efficient than O(n^2)
reverse(){
Node current = this.head;
int half = this.size/2;
int midElement = this.size % 2 == 0 ? 0: half + 1;
Stack<Node<E>> stack = new Stack<Node<E>>();
for(int i = 0; i < this.size; i++){
if (i < = half)
stack.push(current);
else{
if (i == midElement)
continue;
else
swap(stack.pop(), current);
current = current.next;
}
}
swap(Node<E> v, Node<E> v1){
E tmp = v.value;
v.value = v1.value;
v1.value = tmp;
}
This is a little bit of pseudo java. It is missing still the checks for size = 0 or size = 1 when it should return immediately. One for loop. Time Complexity O(n). There is also the need to check when size = 2, swap(...) is to be invoked directly.
Based on the #efekctive 's idea, there a solution. The complexity is a little bit worse than O^2 but no need changes in the swap method, no need in usage of another collection. The code below passes the unit test, however, be careful to use it there could be a bug related to size/2 operation. Hope this help.
public void reverse() {
Node<E> current = head;
SimpleLinkedList<E> firstHalf = new SimpleLinkedList<>();
SimpleLinkedList<E> secondHalf = new SimpleLinkedList<>();
for (int i = 0; i < size; i++) {
if (i >= size / 2) {
firstHalf.add(current.val);
} else {
secondHalf.add(current.val);
}
current = current.next;
}
SimpleLinkedList<E> secondHalfReverse = new SimpleLinkedList<>();
for (int i = 0; i < secondHalf.size(); i++) {
secondHalfReverse.add(secondHalf.get(i));
}
for (int i = 0; i < size / 2; i++) {
if (secondHalfReverse.get(i) == firstHalf.get(i)) {
break;
}
swap(secondHalfReverse.get(i), firstHalf.get(i));
}
}
I am implementing a sorted circular linked list by first populating the list with the sorted elements and also implemented insert and delete functionality to it. However on calling delete it is not updating the list. I tried debugging the code in delete method but no success yet. below is the code snippet of my program.
class CNode {
public int data;
public CNode next;
public CNode() {
this.data = 0;
this.next = null;
}
public CNode(int data, CNode next) {
this.data = data;
this.next = next;
}
public CNode(int data) {
this.data = data;
this.next = null;
}
}
AND the driver class -
public class SortedCLL {
public static CNode head = new CNode();
public static CNode last = new CNode();
public static int NoN;
public SortedCLL() {
int N = 3;
int val[] = {4, 2, 6};
Arrays.sort(val);
CNode first = new CNode(val[0]);
head.next = first;
last.next = first;
NoN++;
for (int i = 1; i < N; i++) {
CNode n = new CNode(val[i]);
last.next.next = n;
last.next = n;
n.next = head.next;
NoN++;
}
//DELETING AN ELEMENT
delete(2);
//INSERTING AN ELEMENT
insert(7);
CNode check = head.next;
for (int i = 0; i < NoN; i++) {
System.out.print(check.data + " ");
check = check.next;
}
}
public static void main(String args[]) throws Exception {
new SortedCLL();
}
private void insert(int element) {
CNode n = new CNode(element);
if(element < head.next.data) {
n.next = head.next;
head.next = n;
last.next.next = n;
NoN++;
return;
}
int nodes = NoN - 1;
CNode iter = head;
while(nodes-- > 0){
if(iter.data < element && iter.next.data > element) {
n.next = iter.next;
iter.next = n;
NoN++;
return;
}
}
last.next.next = n;
last.next = n;
n.next = head.next;
NoN++;
return;
}
private void delete(int element) {
//System.out.println( " : " +element);
CNode prev = last.next;
CNode iter = head.next;
int nodes = NoN;
while(nodes-- > 0) {
if(iter.data == element) {
//HERE IT IS PRINTING CORRECT PREV AND NEXT NODE'S DATA.
System.out.println( prev.data + " : " +iter.next.data);
prev.next = iter.next;
NoN--;
return;
}
prev = iter;
iter = iter.next;
}
return;
}
}
Check the debug statement in the SortedCLL class, in delete method, it is printing correct values of prev and next, still the output is not as expected.
expected list
4 6 7
program's list
2 4 6 7
Any help appreciated !
You are forgetting the edge case of removing the head of the list that requires doing head = head.next; on removal.
Also you're not really handling the case of an empty list anywhere so watch out for that!
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;
}
I have created my own custom Linked List (code is below). Now, I can't understand how to create an array of that Linked list like LinkedList[] l = new LinkedList[10]. Can anyone help me.
class Node {
public int data;
public Node pointer;
}
class LinkedList {
Node first;
int count = 0;
public void addToEnd(int data){
if(first == null){
Node node = new Node();
node.data = data;
node.pointer = null;
first = node;
count = 1;
return;
}
Node next = first;
while(next.pointer != null){
next = (Node)next.pointer;
}
Node newNode = new Node();
newNode.data = data;
newNode.pointer = null;
next.pointer = newNode;
count++;
}
public Node getFirst(){
return first;
}
public Node getLast(){
Node next = first;
while(next.pointer != null)
next = next.pointer;
return next;
}
public int[] get(){
if(count != 0){
int arr[] = new int [count] ;
Node next = first;
int i = 0;
arr[0]= next.data;
while(next.pointer != null){
next = next.pointer;
i++;
arr[i] = next.data;
}
i++;
return arr ;
}
return null ;
}
public int count(){
return count;
}
}
I'm going to guess that your problem is just that when you create an array of objects, like
LinkedList[] lists = new LinkedList[10];
you get an array full of nulls; you need to create objects to store in the array:
for (int i=0; i<lists.length; ++i)
lists[i] = new LinkedList();