Remove a Node from a Singly Linked List

Remove the first node from a singly linked list:

	public void deleteFirst() {
		if(head==null) {
		  return;
		}
		Node current = head;
		head = head.next;
		current.next = null;
	}

 


Java Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*Node of a single linked list of integer */
public class SinglyLinkedList {
	private static Node head; /* instance variable */

	private static class Node {
		private int data; /* we assume data integer */
		private Node next; /* Create a node with the given data and next node */

		public Node(int data) {
			this.data = data;
			this.next = null;
		}
	}
	
	/* Display the single linked list */
	public void display() {
		Node current = head;
		while (current != null) {
			System.out.print(current.data + " --> ");
			current = current.next;
		}
		System.out.println("null");
	}

	//remove first 
	public void removeFirst() {
		if(head==null) {
		  return;
		}
		Node current = head;
		head = head.next;
		current.next = null;
	}

	public static void main(String[] args) {
		SinglyLinkedList s = new SinglyLinkedList();
		s.head = new Node(10);
		Node second = new Node(7);
		Node third = new Node(20);
		Node fourth = new Node(8);

		s.head.next = second; // 10 --> 7
		second.next = third; // 10 -->7 -->20
		third.next = fourth; // 10 -->7 --> 20 -->8 -->null
		System.out.println("Before remove:");
		s.display();
		
		System.out.println("After remove:");
		s.removeFirst(); // 7 --> 20 -->8 -->null
		s.display();

	}

}

Output

Before remove:
10 --> 7 --> 20 --> 8 --> null
After remove:
7 --> 20 --> 8 --> null

Remove the last node from a singly linked list:

	public Node removeLast() {
		if (head == null || head.next == null) {
			return null;
		}
		Node current = head;
		while (current.next.next != null) {
			current = current.next;
		}
		current.next = null;
		return head;
	}

Java Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*Node of a single linked list of integer */
public class SinglyLinkedList {
	private static Node head; /* instance variable */

	private static class Node {
		private int data; /* we assume data integer */
		private Node next; /* Create a node with the given data and next node */

		public Node(int data) {
			this.data = data;
			this.next = null;
		}
	}

	/* Display the single linked list */
	public void display() {
		Node current = head;
		while (current != null) {
			System.out.print(current.data + " --> ");
			current = current.next;
		}
		System.out.println("null");
	}

	// remove last
	public Node removeLast() {
		if (head == null || head.next == null) {
			return null;
		}
		Node current = head;
		while (current.next.next != null) {
			current = current.next;
		}
		current.next = null;
		return head;
	}

	public static void main(String[] args) {
		SinglyLinkedList s = new SinglyLinkedList();
		s.head = new Node(10);
		Node second = new Node(7);
		Node third = new Node(20);
		Node fourth = new Node(8);

		s.head.next = second; // 10 --> 7
		second.next = third; // 10 -->7 --> 20
		third.next = fourth; // 10 -->7 --> 20 --> 8 -->null
		System.out.println("Before remove:");
		s.display();

		System.out.println("After remove:");
		head = s.removeLast(); // 10 -->7 --> 20 -->null
		s.display();

	}

}

Output

Before remove:
10 --> 7 --> 20 --> 8 --> null
After remove:
10 --> 7 --> 20 --> null

Remove a node from a singly linked list at a given position:

public void removePosition(int pos) {
		if (head == null) {
			return;
		}	
		if (pos == 1) {
			head = head.next;
			return;
		}
		Node privious = head;
		int currentPos = 1;
		while (currentPos < pos - 1) {
			privious = privious.next;
			currentPos++;
		}
		Node corrent = privious.next;
		privious.next = corrent.next;

	}

Java Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*Node of a single linked list of integer */
public class SinglyLinkedList {
	private static Node head; /* instance variable */

	private static class Node {
		private int data; /* we assume data integer */
		private Node next; /* Create a node with the given data and next node */

		public Node(int data) {
			this.data = data;
			this.next = null;
		}
	}

	/* Display the single linked list */
	public void display() {
		Node current = head;
		while (current != null) {
			System.out.print(current.data + " --> ");
			current = current.next;
		}
		System.out.println("null");
	}

	// remove a Linked List node at a given position
	public void removePosition(int pos) {
		// If linked list is empty 
		if (head == null) {
			return;
		}
		// If head needs to be removed 
		if (pos == 1) {
			head = head.next;
			return;
		}
		Node privious = head;
		int currentPos = 1;
		while (currentPos < pos - 1) {
			privious = privious.next;
			currentPos++;
		}
		Node corrent = privious.next;
		privious.next = corrent.next;

	}

	public static void main(String[] args) {
		SinglyLinkedList s = new SinglyLinkedList();
		s.head = new Node(10);
		Node second = new Node(7);
		Node third = new Node(20);
		Node fourth = new Node(8);

		s.head.next = second; // 10 --> 7
		second.next = third; // 10 -->7 --> 20
		third.next = fourth; // 10 -->7 --> 20 --> 8 -->null
		System.out.print("Before remove: ");
		s.display();

		System.out.println("After remove:");
		System.out.print("remove head: ");
		s.removePosition(1);
		s.display(); // 7 --> 20 --> 8 --> null
		System.out.print("remove node position 2: ");
		s.removePosition(2);
		s.display(); // 7 --> 8 --> null

	}

}

Output

Before remove: 10 --> 7 --> 20 --> 8 --> null
After remove:
remove head: 7 --> 20 --> 8 --> null
remove node position 2: 7 --> 8 --> null

You may also like...
Singly Linked Lists
Insert node at the beginning of a Singly Linked List
Insert a Node at a Specific Position in a Linked List
Find length of a Singly Linked List
Add a node at the end of a Singly Linked List

Next Post Previous Post
No Comment
Add Comment
comment url